2

ビデオをダウンロードして分離ストレージに保存する必要がある Phonegap を使用して WP7.1 用のアプリケーションを作成しています。そのビデオを読んでいるときに、初めて正しく読むことができますが、その後、ストリームを読むことができません。この例外は、一度読んだ後にそのビデオを読み込もうとするたびに発生します:操作は、IsolatedStorageFileStream では許可されていません。

からコードを取得: 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 関数で発生します (上記のコードの ** を参照してください)。初めて完全に実行され、2回目にファイルを読み取ると、例外が発生します。

何が問題なのですか?私は何か間違ったことをしていますか?

4

2 に答える 2

2

ファイルは前回の読み取りからまだ開いているようです。この場合、fileAccessとfileShareを指定して、別のスレッドで開くことができるようにする必要があります。

using(IsolatedStorageFileStream stream = new IsolationStorageFileStream(filePath、FileMode.Open、FileAccess.Read、FileShare.Read、isoFile)

于 2012-06-15T07:13:48.000 に答える
1

戻る前にMediaElementのsourceプロパティを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 に答える