Windows Phone 8 Development Internals: Phone and Media Services

  • 6/15/2013

Music and Videos Hub

The Music and Videos Hub on Windows Phone is a centralized location for accessing the phone’s music and videos library. Figure 5-13 shows an app that integrates with the Music and Videos Hub to fetch a list of all songs in the library and render them in a ListBox. This is the TestMediaHub solution in the sample code. When the user selects an item from the ListBox, the app fetches the selected song’s album art and presents buttons with which he can play/pause the selected song.

Figure 5-13

Figure 5-13 Your app can integrate with the Music and Videos Hub on the phone.

The MainPage class declares fields for the MediaLibrary itself and for the current Song. As always, you need to ensure that you pump the XNA message queue. Next, we initialize the MediaLibrary field and set the collection of Songs to be the ItemsSource on your ListBox. In the XAML, we data-bind the Text property on the ListBox items to the Name property on each Song.

private MediaLibrary library;
private Song currentSong;
public MainPage()
    Play = ApplicationBar.Buttons[0] as ApplicationBarIconButton;
    Pause = ApplicationBar.Buttons[1] as ApplicationBarIconButton;
    DispatcherTimer dt = new DispatcherTimer();
    dt.Interval = TimeSpan.FromMilliseconds(33);
    dt.Tick += delegate { FrameworkDispatcher.Update(); };
    library = new MediaLibrary();
    HistoryList.ItemsSource = library.Songs;

You can’t effectively test this on the emulator, because it has no songs. If you test on a physical device, you would want one that has a representative number of songs. If there are very many songs on the device, initializing the list could be slow—and in this case you could restrict the test to perhaps the first album by using the following syntax:

HistoryList.ItemsSource = library.Albums.First().Songs;

The app’s play and pause operations are more or less self-explanatory, invoking the MediaPlayer Play or Pause methods.

private void Play_Click(object sender, EventArgs e)
    Pause.IsEnabled = true;
    Play.IsEnabled = false;
private void Pause_Click(object sender, EventArgs e)
    Play.IsEnabled = true;

The interesting work is done in the SelectionChanged handler for the ListBox. Here, we fetch the currently selected item and fetch its album art to render in an Image control in the app’s UI. If we can’t find any corresponding album art, we use a default image built in to the app.

private void HistoryList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    if (HistoryList.SelectedIndex == -1)
    currentSong = HistoryList.SelectedItem as Song;
    if (currentSong != null)
        Play.IsEnabled = true;
        Stream albumArtStream = currentSong.Album.GetAlbumArt();
        if (albumArtStream == null)
            StreamResourceInfo albumArtPlaceholder =
                App.GetResourceStream(new Uri(
                    "Assets/AlbumArtPlaceholder.jpg", UriKind.Relative));
            albumArtStream = albumArtPlaceholder.Stream;
        BitmapImage albumArtImage = new BitmapImage();
        MediaImage.Source = albumArtImage;