Playing a Song
The code in this tutorial illustrates the technique for playing a song from a library. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.
The Albums property provides access to the media library, and the Play method plays a song. Consider any current audio playback when using the Play method. If the user currently is playing a different song, the Stop method can be used to stop the current song.
To play a song from a random album in a user's media library
The following demonstrates how to play a song from a randomly picked album (shuffle).
- MediaPlayer Class
- Provides methods and properties to play, pause, resume, and stop songs. MediaPlayer also exposes shuffle, repeat, volume, play position, and visualization capabilities.
- MediaLibrary Class
- Provides access to songs, playlists, and pictures in the device's media library.
- MediaPlayer.Play Method
- Plays a song or collection of songs.
- MediaLibrary.Albums Property
- Gets the AlbumCollection that contains all albums in the media library.