2

我正在使用 Phonegap 为 WP7.1 创建一个应用程序,我必须在其中下载视频并将其保存在独立存储中。现在在阅读该视频时,我第一次可以正确阅读它,但之后我无法阅读流。每次我在读过该视频后尝试阅读该视频时都会发生此异常: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;
            }
        }
    }
}

** 读取文件时的 _Play 函数发生异常(Operation not allowed on IsolatedStorageFileStream.)(请参见上面代码中的**)。第一次运行完美,当我第二次读取文件时,它给出了异常。

可能是什么问题?我做错了什么吗?

4

2 回答 2

2

听起来该文件仍然从先前的读取中打开。如果是这种情况,您需要指定 fileAccess 和 fileShare 以允许它被另一个线程打开:

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

于 2012-06-15T07:13:48.000 回答
1

我通过在返回之前将 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;

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

        }

谢谢大家。

于 2012-06-15T11:31:55.353 回答