How to read an audio file from IsolatedStorage?

主宰稳场 提交于 2019-12-14 04:09:01

问题


I have an audio file stored in IsolatedStorage..

I want to access it by calling a method of another class:

using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream fileStream = isolatedStorage.OpenFile(filePath, FileMode.Open, FileAccess.Read))
    {

        return fileStream;
    }
}

now when I call that method this way:

var fileStream = Musics.TryGetMusic("DaDaDa.mp3");
musicMediaElement.SetSource(fileStream);
musicMediaElement.Play();

I get an error saying it cannot read a closed file.

The cause is that I'm using using statement and the file is closed when I call Play(). How can I solve this problem?


回答1:


It's because, I presume yo call it, like

.... 

 using (IsolatedStorageFileStream fileStream = isolatedStorage.OpenFile(filePath, FileMode.Open, FileAccess.Read))
 {    
     return fileStream;
 }
....

After exiting from the using statement, fileStream instance will be disposed.

To resolve this issue should be enough to not use using here,but instead track that instance lifetime and call dispose manually in appropriate place.



来源:https://stackoverflow.com/questions/19008483/how-to-read-an-audio-file-from-isolatedstorage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!