简体   繁体   English

如何从IsolatedStorage中读取音频文件?

[英]How to read an audio file from IsolatedStorage?

I have an audio file stored in IsolatedStorage .. 我有一个音频文件存储在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() . 原因是我正在使用using语句,并且在我调用Play()时文件已关闭。 How can I solve this problem? 我怎么解决这个问题?

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. using语句退出后,将处理fileStream实例。

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. 要解决这个问题应该是足够的,不使用using在这里,而是跟踪实例寿命和呼叫处理手动在适当的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM