【发布时间】:2012-06-15 05:36:54
【问题描述】:
我正在使用 Phonegap 为 WP7.1 创建一个应用程序,我必须在其中下载视频并将其保存在独立存储中。 现在在阅读该视频时,我第一次可以正确阅读它,但之后我无法阅读该流。每次我在阅读该视频后尝试阅读该视频时都会出现此异常:Operation not allowed on IsolatedStorageFileStream.
代码来自:How to play embedded video in WP7 - Phonegap? 并添加了暂停和停止功能。
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using WP7CordovaClassLib.Cordova.JSON;
namespace WP7CordovaClassLib.Cordova.Commands
{
public class Video : BaseCommand
{
/// <summary>
/// Video player object
/// </summary>
private MediaElement _player;
Grid grid;
[DataContract]
public class VideoOptions
{
/// <summary>
/// Path to video file
/// </summary>
[DataMember(Name = "src")]
public string Src { get; set; }
}
public void Play(string args)
{
VideoOptions options = JsonHelper.Deserialize<VideoOptions>(args);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
try
{
_Play(options.Src);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
GoBack();
}
});
}
private void _Play(string filePath)
{
if (_player != null)
{
if (_player.CurrentState == System.Windows.Media.MediaElementState.Paused)
{
_player.Play();
}
}
else
{
// this.player is a MediaElement, it must be added to the visual tree in order to play
PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
if (frame != null)
{
PhoneApplicationPage page = frame.Content as PhoneApplicationPage;
if (page != null)
{
grid = page.FindName("VideoPanel") as Grid;
if (grid != null && _player == null)
{
_player = new MediaElement();
grid.Children.Add(this._player);
grid.Visibility = Visibility.Visible;
_player.Visibility = Visibility.Visible;
_player.MediaEnded += new RoutedEventHandler(_player_MediaEnded);
}
}
}
Uri uri = new Uri(filePath, UriKind.RelativeOrAbsolute);
if (uri.IsAbsoluteUri)
{
_player.Source = uri;
}
else
{
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoFile.FileExists(filePath))
{
**using (IsolatedStorageFileStream stream =
new IsolatedStorageFileStream(filePath, FileMode.Open, isoFile))
{
_player.SetSource(stream);
stream.Close();
}
}
else
{
throw new ArgumentException("Source doesn't exist");
}
}
}
_player.Play();
}
}
void _player_MediaEnded(object sender, RoutedEventArgs e)
{
GoBack();
}
public void Pause(string args)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
try
{
_Pause(args);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
}
});
}
private void _Pause(string filePath)
{
if (_player != null)
{
if (_player.CurrentState == System.Windows.Media.MediaElementState.Playing)
{
_player.Pause();
}
}
}
public void Stop(string args)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
try
{
_Stop(args);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e.Message));
}
});
}
private void _Stop(string filePath)
{
GoBack();
}
private void GoBack()
{
if (_player != null)
{
if (_player.CurrentState == System.Windows.Media.MediaElementState.Playing
|| _player.CurrentState == System.Windows.Media.MediaElementState.Paused)
{
_player.Stop();
}
_player.Visibility = Visibility.Collapsed;
_player = null;
}
if (grid != null)
{
grid.Visibility = Visibility.Collapsed;
}
}
}
}
** 异常(Operation not allowed on IsolatedStorageFileStream.)发生在读取文件时的 _Play 函数中(请参见上面代码中的 **)。第一次运行完美,第二次读取文件时出现异常。
可能是什么问题? 我是不是做错了什么?
【问题讨论】:
-
在隔离存储中存储数据的代码在哪里?另外,您得到的确切错误(异常)是什么?在哪一行代码?
-
流流REsponse = Iresponse.GetResponseStream();使用 (BinaryReader bReader = new BinaryReader(streamREsponse)) { content = bReader.ReadBytes((int)streamREsponse.Length); } using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var file = new IsolatedStorageFileStream(filePath, FileMode.Create, store)) { file.Write(content, 0, content.Length); } }
-
您好 nkchandra,我保存了从 url 下载后获得的视频流。 (请参阅上面的评论)。谢谢
标签: c# windows-phone-7 cordova dispatcher isolatedstorage