2
votes

I am creating an application for WP7.1 with Phonegap in which I have to download a video and save it in Isolated Storage. Now while reading that video, for the first time I can read it correctly, but after that I am not able to read the stream. This exception occurs every I try to read that video after I have read it once : Operation not permitted on IsolatedStorageFileStream.

Taken the code from : How to play embedded video in WP7 - Phonegap? and added Pause and Stop functionality.

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;
            }
        }
    }
}

** The exception (Operation not permitted on IsolatedStorageFileStream.) occurs at _Play function while reading the file (Please see ** in code above). First time it runs perfectly and when I come to read the file second time it gives Exception.

What might be the problem? Is I am doing something wrong?

2
where is the code to store the data in the isolated storage? Also what is the exact error(excetion) you are getting? and at which line of code?nkchandra
Stream streamREsponse = Iresponse.GetResponseStream(); using (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); } }Deeps
Hello nkchandra, I have saved video stream which i got after downloading from a url. (Please see above comment). ThanksDeeps

2 Answers

2
votes

It sounds like the file is still open from the previous read. If this is the case, you need to specify the fileAccess and fileShare to allow it to be opened by another thread:

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

1
votes

I solved this problem by just setting the source property of the MediaElement to null before navigating back. So, when i come back to play the same video, the MediaElement source is free for it.

Edited the GoBack Function to :

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

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

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

        }

Thanks All.