MediaLibrary Class

Provides access to songs, playlists, and pictures in the device's media library.

Namespace: Microsoft.Xna.Framework.Media
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)


public sealed class MediaLibrary : IDisposable


MediaLibrary provides the following properties that return media collections: Albums, Artists, Genres, Pictures, Playlists, and Songs. Each property returns a collection object that can be enumerated and indexed. The collection object represents all media of that type in the device's media library.

The media collections do not retrieve or instantiate all media objects immediately. Instead, you can use the collections when needed to retrieve individual media objects such as Song and Artist objects.

On Windows, MediaLibrary can find songs only if the Windows Media Player previously found songs on the system. This means that Windows Media Player first must search the system for music before any songs can be accessed through MediaLibrary.

Dd254846.bp(en-us,XNAGameStudio.41).gifBest Practice

Because MediaLibrary implements the IDisposable interface, instantiation and use of the MediaLibrary object should be enclosed within a using block.


The following sample fragment demonstrates how to use MediaLibrary to retrieve and play a song.

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace MobileSimpleSongPlay
    static class Program
        static void Main(string[] args)
            using(MediaLibrary library = new MediaLibrary())
                SongCollection songs = library.Songs;
                Song song = songs[0];
            while (!GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Back))
            { ; }


You may get an exception when you try to play DRM protected music.

See Also


Accessing Pictures from a Picture Album


MediaLibrary Members
Microsoft.Xna.Framework.Media Namespace


Xbox 360, Windows 7, Windows Vista, Windows XP, Windows Phone