|
In this article we will discuss about the MediaLibrary of Windows Phone 7. MediaLibrary provides details of songs, albums, artists, genres and pictures.
Songs Property
MediaLibrary has Songs property to get the SongCollection available in the media library.
Add below textblocks in MainPage.xaml.
<TextBlock Height="30" HorizontalAlignment="Left" Margin="10,20,0,0" Name="sName" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,60,0,0" Name="sAlbumName" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,100,0,0" Name="sArtistName" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,140,0,0" Name="sDuration" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,180,0,0" Name="sGenre" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,220,0,0" Name="sPlayCount" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,260,0,0" Name="sRating" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,300,0,0" Name="sTrackNumber" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,340,0,0" Name="sSongsCount" VerticalAlignment="Top" FontSize="18" />
Song class provides access to song in the song library. One can get song name, album in which song belong to, artist in which song belong to, duration of the song, genre of the song, playcount of the song, user's rating of the song, tracknumber of the song.
SongCollection class can be used to get number of songs in medialibrary.
Place below code in the codebehind file.
using (MediaLibrary library = new MediaLibrary()) { SongCollection songs = library.Songs; Song song = songs[0]; sName.Text = "Song Name: " + song.Name; sAlbumName.Text = "Album Name: " + song.Album.Name; sArtistName.Text = "Artist Name:" + song.Artist.Name; sDuration.Text = "Duration:" + song.Duration.Seconds.ToString(); sGenre.Text = "Genre: " + song.Genre.Name; sPlayCount.Text = "PlayCount: " + song.PlayCount.ToString(); sRating.Text = "Rating: " + song.Rating.ToString(); sTrackNumber.Text = "Track Number: " + song.TrackNumber.ToString(); sSongsCount.Text = "Total songs available in media library: " + songs.Count.ToString(); }

Albums Property
MediaLibrary has albums property to get the AlbumCollection in the media library.
Add below textblocks in MainPage.xaml
<TextBlock Height="30" HorizontalAlignment="Left" Margin="10,20,0,0" Name="aName" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,60,0,0" Name="aArtistName" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,100,0,0" Name="aDuration" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,140,0,0" Name="aGenreName" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,180,0,0" Name="aSongsCount" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,220,0,0" Name="aAlbumsCount" VerticalAlignment="Top" FontSize="18" />
Album class provides access to album in the media library. One can get album name, album artist name, album duration, album genre, album song count.
AlbumCollection provided total number of albums in the medial library.
Place below code in MainPage.xaml.cs.
using (MediaLibrary library = new MediaLibrary()) { AlbumCollection albums = library.Albums; Album album = albums[0]; aName.Text = "Album Name: " + album.Name; aArtistName.Text = "Artist Name: " + album.Artist.Name; aDuration.Text = "Duration: "+ album.Duration.Seconds.ToString(); aGenreName.Text = "Genre: " + album.Genre.Name; aSongsCount.Text = "Count: " + album.Songs.Count.ToString(); aAlbumsCount.Text = "Total albums available in media library: " + albums.Count.ToString(); }
Artists Property
MediaLibrary has Artists property to get the ArtistCollection in the media library.
Place below code in the MainPage.xaml.
<TextBlock Height="30" HorizontalAlignment="Left" Margin="10,20,0,0" Name="arName" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,60,0,0" Name="arAlbumsCount" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,100,0,0" Name="arSongsCount" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,140,0,0" Name="arCount" VerticalAlignment="Top" FontSize="18" />
Artist class provides access to artist name, artist album count and artists songs count.
ArtistCollection class provides number of artists available in media library.
Place below code in MaingPage.xaml.cs
using (MediaLibrary library = new MediaLibrary()) { ArtistCollection artists = library.Artists; Artist artist = artists[0]; arName.Text = "Artist Name: " + artist.Name; arAlbumsCount.Text = "Artist Album Count: " + artist.Albums.Count.ToString(); arSongsCount.Text = "Artist Songs Count: " + artist.Songs.Count.ToString(); arCount.Text = "Total artists available in media library: " + artists.Count.ToString(); }

Genres Property
MediaLibrary has genres property to get GenreCollection of media library.
<TextBlock Height="30" HorizontalAlignment="Left" Margin="10,20,0,0" Name="gName" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,60,0,0" Name="gAlbumsCount" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,100,0,0" Name="gSongsCount" VerticalAlignment="Top" FontSize="18" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="10,140,0,0" Name="gCount" VerticalAlignment="Top" FontSize="18" />
Genre class provides access to genre name, genre album count and genre songs count.
GenreCollection provides total numbe of genres available in the medial library.
Place below code in the codebehind.
using (MediaLibrary library = new MediaLibrary()) { GenreCollection genres = library.Genres; Genre genre = genres[0]; gName.Text = "Genre Name: " + genre.Name; gAlbumsCount.Text = "Genre Album Count: " + genre.Albums.Count.ToString(); gSongsCount.Text = "Genre Songs Count: " + genre.Songs.Count.ToString(); gCount.Text = "Total genres available in media library: " + genres.Count.ToString(); }

This ends the article of getting details of Albums, artists, songs and genres in media library.
|