Search code examples
c#windows-phone-7isolatedstoragemediaelement

Playing media file from Isolated Storage in windows phone?


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


Solution

  • 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.