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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks @Tigran. How can I dispose it manually while that is a method of another class (helper) Musics.TryGetMusic() and I have not access to it anymore?
@user2799350: You return it to caller. So caller can take care of tracking it.
So in my question above, I should dispose later var fileStream =....
@user2799350: exactly. when you finish using it and you no more need it (in your case probably after Play(..)), call dispose on it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.