,

Saving Music to the Media Library

In Windows Phone 8 you can now add and remove music files from the user’s music collection with the SaveSong and Delete methods that extend the MediaLibrary class. This allows you to create feature-rich music apps that tightly integrate with the phone’s Music+Videos Hub.

This API is found in the Microsoft.Xna.Framework.Media.PhoneExtensions namespace in the assembly Microsoft.Xna.Framework.MediaLibraryExtensions.dll.

The sample for this section is located in the MediaLibrary directory of the WPUnleashed.Examples project, in the downloadable sample code.

To add a track to the media library, place the file in isolated storage and create an instance of the SongMetadata class to describe the attributes of the track. This includes album and artist details as well as album art. The following excerpt from the sample’s MediaLibraryViewModel class saves a track’s audio file and image to isolated storage. It then calls the MediaLibrary class’s SaveSong method to add the track to the Music+Videos Hub, as shown:

void SaveSong()
{
    CopyAudioToStorage();

    Uri songUri = new Uri("MediaLibrary/Sample.wma", UriKind.Relative);
    Uri artUri = new Uri("MediaLibrary/Sample.jpg", UriKind.Relative);

    SongMetadata metadata = new SongMetadata
        {
            Name = "Unleashed Jingle",
            AlbumName = "Dev Hits 2013",
            AlbumArtUri = artUri
        };

    MediaLibrary mediaLibrary = new MediaLibrary();
    mediaLibrary.SaveSong(songUri, metadata, SaveSongOperation.CopyToLibrary);

    MessageService.ShowMessage("'Unleashed Jingle' is now located "
                        + "in the Music section of the Music+Pictures Hub.");
}

The SaveSong method accepts a SaveSongOperation argument that can be either CopyToLibrary or MoveToLibrary. MoveToLibrary deletes the file from isolated storage on completion, whereas CopyToLibrary leaves the source file in its current location.

After it is saved, the track appears in the Music+Videos Hub, as shown in Figure 7.7.

Image

FIGURE 7.7 A track is added to the Music+Pictures Hub.

The MediaLibrary class allows you to retrieve the list of songs from the Music+Videos Hub and to delete items from the list. The following excerpt from the MediaLibraryViewModel retrieves a previously added track from the Songs collection using LINQ, and removes the song by calling the MediaLibrary class’s Delete method:

void DeleteSong()
{
    MediaLibrary mediaLibrary = new MediaLibrary();
    Song savedSong = mediaLibrary.Songs.SingleOrDefault(
        song => song.Name == "Unleashed Jingle");

    if (savedSong == null)
    {
        MessageService.ShowMessage("Song not found.");
        return;
    }

    mediaLibrary.Delete(savedSong);
}

The Delete method presents a built-in confirmation dialog to the user before the item is physically removed from the media library. This prevents apps from indiscriminately deleting items and gives the user peace of mind.

The Delete method performs a cascading delete, so that if the song is the last song in the Album and the Album is the last Album for an Artist, the Album and Artist are also deleted from the media library.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.190.58