Search code examples
c#windows-phone-7windows-phone-8playbackmedia-library

How to Get a Specific Song From the Phone's MediaLibrary? And optimising the code


In my application I have the following code, which get's the phone's MediaLibrary and filters out the correct chosen Song from the MediaLibrary.Songs and plays it:

using (MediaLibrary library = new MediaLibrary())
{
    foreach (var item in library.Songs)
    {
        if (item.Name == songName)
        {
            FrameworkDispatcher.Update();
            MediaPlayer.Play(item);
        }
    }
    library.Dispose();
}  

However this takes quite a while and results in a pause. Is there a faster/more-efficient way to access a specific Song from the phone's MediaLibrary.Songs?

Thanks for your help.


Solution

  • Maybe you have to store the values in a XML document after you read the directory, this can save you a lot of time if the library doesn't get changed (Check the size of the whole library). If it does then you can check the library again and update your XML.