【问题标题】:Isolated Storage Exception while reading a stream next time下次读取流时出现隔离存储异常
【发布时间】: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


【解决方案1】:

听起来该文件在上次读取时仍处于打开状态。如果是这种情况,您需要指定 fileAccess 和 fileShare 以允许它被另一个线程打开:

使用 (IsolatedStorageFileStream 流 = new IsolatedStorageFileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, isoFile)

【讨论】:

  • 没错。那是因为 MediaElement 使文件保持打开状态,因此您无法以独占访问第二次打开它。 blogs.codes-sources.com/kookiz/archive/2012/05/06/…
  • 您好乔恩,感谢您的回复。我试过这个,现在我没有得到那个例外,但我也无法观看视频。首先,一切正常。但是当我第二次播放视频时,什么都没有显示,也没有发生错误或异常。
【解决方案2】:

我通过在返回之前将 MediaElement 的源属性设置为 null 解决了这个问题。所以,当我回来播放相同的视频时,MediaElement 源是免费的。

将 GoBack 函数编辑为:

private void GoBack()
        {
            // see whole code from original question.................

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

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

        }

谢谢大家。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-19
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多