C# 下次读取流时发生隔离存储异常

C# 下次读取流时发生隔离存储异常,c#,windows-phone-7,cordova,dispatcher,isolatedstorage,C#,Windows Phone 7,Cordova,Dispatcher,Isolatedstorage,我正在使用Phonegap为WP7.1创建一个应用程序,在这个应用程序中,我必须下载一个视频并将其保存在独立的存储中。 现在,在阅读视频时,我第一次能够正确地阅读视频,但之后我无法阅读视频流。每次我在阅读该视频一次后尝试阅读该视频时,都会出现此异常:不允许对IsolatedStorageFileStream执行操作。 代码取自: 并增加了暂停和停止功能 using System; using System.IO; using System.IO.IsolatedStorage; using Sy

我正在使用Phonegap为WP7.1创建一个应用程序,在这个应用程序中,我必须下载一个视频并将其保存在独立的存储中。 现在,在阅读视频时,我第一次能够正确地阅读视频,但之后我无法阅读视频流。每次我在阅读该视频一次后尝试阅读该视频时,都会出现此异常:不允许对IsolatedStorageFileStream执行操作。

代码取自: 并增加了暂停和停止功能

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;
            }
        }
    }
}
使用系统;
使用System.IO;
使用System.IO.IsolatedStorage;
使用System.Runtime.Serialization;
使用System.Windows;
使用System.Windows.Controls;
使用Microsoft.Phone.Controls;
使用WP7CordovaClassLib.Cordova.JSON;
命名空间WP7CordovaClassLib.Cordova.Commands
{
公共类视频:BaseCommand
{
/// 
///视频播放器对象
/// 
私有媒体元素播放器;
网格;
[数据合同]
公共类视频选项
{
/// 
///视频文件的路径
/// 
[DataMember(Name=“src”)]
公共字符串Src{get;set;}
}
公共无效播放(字符串参数)
{
VideoOptions=JsonHelper.Deserialize(args);
Deployment.Current.Dispatcher.BeginInvoke(()=>
{
尝试
{
_播放(options.Src);
DispatchCommandResult(新的PluginResult(PluginResult.Status.OK));
}
捕获(例外e)
{
DispatchCommandResult(新的PluginResult(PluginResult.Status.ERROR,e.Message));
GoBack();
}
});
}
私有void\u播放(字符串文件路径)
{
如果(_player!=null)
{
if(_player.CurrentState==System.Windows.Media.MediaElementState.Paused)
{
_player.Play();
}
}
其他的
{
//this.player是一个MediaElement,必须将其添加到视觉树中才能播放
PhoneApplicationFrame=Application.Current.RootVisual作为PhoneApplicationFrame;
如果(帧!=null)
{
PhoneApplicationPage页面=框架。内容为PhoneApplicationPage;
如果(第页!=null)
{
网格=页面。FindName(“视频面板”)作为网格;
如果(网格!=null&&u玩家==null)
{
_player=新媒体元素();
grid.Children.Add(这个._播放器);
grid.Visibility=可见性.Visibility;
_player.Visibility=可见性.Visibility;
_player.MediaEnded+=新的路由EventHandler(\u player\u MediaEnded);
}
}
}
Uri=新Uri(文件路径,UriKind.RelativeOrAbsolute);
if(uri.euri)
{
_player.Source=uri;
}
其他的
{
使用(IsolatedStorageFile isoFile=IsolatedStorageFile.GetUserStoreForApplication())
{
if(isoFile.FileExists(filePath))
{
**使用(隔离存储文件流)=
新的IsolatedStorageFileStream(文件路径、文件模式、打开、isoFile))
{
_player.SetSource(流);
stream.Close();
}
}
其他的
{
抛出新ArgumentException(“源不存在”);
}
}
}
_player.Play();
}
}
void _player _mediated(对象发送方,路由目标)
{
GoBack();
}
公共无效暂停(字符串参数)
{
Deployment.Current.Dispatcher.BeginInvoke(()=>
{
尝试
{
_暂停(args);
DispatchCommandResult(新的PluginResult(PluginResult.Status.OK));
}
捕获(例外e)
{
DispatchCommandResult(新的PluginResult(PluginResult.Status.ERROR,e.Message));
}
});
}
私有无效\u暂停(字符串文件路径)
{
如果(_player!=null)
{
if(_player.CurrentState==System.Windows.Media.MediaElementState.Playing)
{
_player.Pause();
}
}
}
公共无效停止(字符串参数)
{
Deployment.Current.Dispatcher.BeginInvoke(()=>
{
尝试
{
_停止(args);
DispatchCommandResult(新的PluginResult(PluginResult.Status.OK));
}
捕获(例外e)
{
DispatchCommandResult(新的PluginResult(PluginResult.Status.ERROR,e.Message));
}
});
}
私有void\u Stop(字符串文件路径)
{
GoBack();
}
私有void GoBack()
{
如果(_player!=null)
{
如果(_player.CurrentState==System.Windows.Media.MediaElementState.Playing
||_player.CurrentState==System.Windows.Media.MediaElementState.Paused)
{
_player.Stop();
}
private void GoBack()
        {
            // see whole code from original question.................

                _player.Visibility = Visibility.Collapsed;
                _player.Source = null; // added this line
                _player = null;

           //..................

        }