media-playerwindows-phone-8.1background-audio

How to access media library in WP 8.1?


Microsoft recommends using BackgroundMediaPlayer to play background audio in WP 8.1

Official sample access to music files:

BackgroundMediaPlayer.Current.SetUriSource(new Uri("ms-appx:///Assets/Media/Ring01.wma"));

But this example does not explain how to access the music library. How to connect to the phone media library, including Artists, Genres, Playlists and Albums? In previous versions (WP7, WP8) Xna.Framework.Media.MediaPlayer was responsible for this purpose. However previous MediaPlayer was not allowed to make a rewind. From the moment I do not understand how to access the full music library to play it with a full-featured useful player.


Solution

  • In WP8.1 there is no direct repleacement of MediaLibrary.

    Use StorageItemContentProperties on StorageFile and call GetMusicPropertiesAsync it returns MusicProperties.

    Then you might have to separate files,

    StorageFolder musicFolder = KnownFolders.MusicLibrary;
    IReadOnlyList<StorageFile> fileList = await musicFolder.GetFilesAsync();
    
    foreach (var file in fileList)
    {
        MusicProperties musicProperties = await file.Properties.GetMusicPropertiesAsync();
        musicProperties.Album;
        musicProperties.Rating;
        musicProperties.Publisher;
    }
    

    Reference