Playing media file from Isolated Storage in window

2019-07-10 13:14发布

问题:

I need to play music file from the isolated storage. i did by this way,

MediaElement media = new MediaElement();

using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())

{
    using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(FileName, FileMode.Open, FileAccess.Read))
    {
         media.SetSource(fileStream);
         media.Play();
    }
}

I can't play the music file. When i created media element Xaml and set source that element this works fine. The problem is, i have to create all controls dynamically.

Pls suggest me how to resolve this pblm...

Thanks

回答1:

You need to add this MediaElement to your visual tree. Or in other words the MediaElement should be part of your PhoneApplicationPage.

Assume you have a Grid inside your page and add this MediaElement to the grid.

grid.Children.Add(media);

And then you can set the Source as well as Play() the media.