Windows Phone中的“IsolatedStorageFileStream上不允许操作”问题

时间:2014-06-06 08:02:23

标签: c# windows-phone-8 isolatedstorage

我已将视频文件保存在IsolatedStorage中,并使用Windows Phone 8中的媒体元素播放它。这是第一次,它执行得非常好并且运行成功,我可以播放视频,

问题是,每当我第二次玩时,我都会收到这样的错误:

"Operation not permitted on IsolatedStorageFileStream"

请参阅我的代码:

 string mediafile = "asaqwrereertrtyrzxxcvcvvbvbv+qwwezzxzxz";
 byte[] PlayByte = Convert.FromBase64String(mediafile);
 MemoryStream stream = new MemoryStream(PlayByte, 0, PlayByte.Length);


 IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(audioName, FileMode.OpenOrCreate,FileAccess.ReadWrite, IsolatedStorageFile.GetUserStoreForApplication());
 isfStream.Write(stream.ToArray(), 0, stream.ToArray().Length);
 isfStream.Close();
 Uri uri = new Uri(isfStream.Name.ToString());
 MediaElement1.Source = uri;
 MediaElement1.Play();

2 个答案:

答案 0 :(得分:1)

应该通过处理该流意味着当音频停止播放时通过isfStream.Dispose();处理流,这样它就不会抛出异常,但仍然是问题仍然可以尝试下面给出的解决方案:

通常会在您同时执行多次代码块时发生。你最终锁定了文件。因此,您必须确保在构造函数中包含FileAccessFileShare模式,如下所示:

using(var isfStream = new IsolatedStorageFileStream(audioName, FileMode.OpenOrCreate,FileAccess.ReadWrite, IsolatedStorageFile.GetUserStoreForApplication())
{
//...
}

答案 1 :(得分:0)

string mediafile = "asaqwrereertrtyrzxxcvcvvbvbv+qwwezzxzxz";
 byte[] PlayByte = Convert.FromBase64String(mediafile);
        MemoryStream stream = new MemoryStream(PlayByte, 0, PlayByte.Length);
        string finalstring = stream.ToString();

        **MediaElement1.Source = null;**
            IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(audioName, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication());
            isfStream.Write(stream.ToArray(), 0, stream.ToArray().Length);
            isfStream.Close();
            Uri uri = new Uri(isfStream.Name.ToString());
            MediaElement1.Source = uri;
            MediaElement1.Play();