0

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?

4

1 回答 1

1

这是因为,我想你这么称呼它,就像

.... 

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

退出using语句后,fileStream实例将被释放。

解决这个问题应该足够了,不要using在这里使用,而是跟踪该实例的生命周期并在适当的地方手动调用 dispose。

于 2013-09-25T15:10:52.660 回答