不允许对isolatedstoragefilestream进行操作;Windows Phone的问题

本文关键字:Windows Phone 问题 操作 isolatedstoragefilestream 不允许 | 更新日期: 2023-09-27 18:07:05

我已将视频文件保存在IsolatedStorage中,并在Windows Phone 8中使用Media Element播放。第一次,它执行得很好,运行成功,我可以播放视频了,

问题是,每当我玩第二次,我得到这样的错误:

"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();

不允许对isolatedstoragefilestream进行操作;Windows Phone的问题

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

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

using(var isfStream = new IsolatedStorageFileStream(audioName, FileMode.OpenOrCreate,FileAccess.ReadWrite, IsolatedStorageFile.GetUserStoreForApplication())
{
//...
}
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();