Question

I'm trying to make a more intuitive music playing app, so my question is: how can I access the music already stored on the phones music library?

I need to get information such as: -song name -song artist -song album -track number

Is there any way to do so?

Was it helpful?

Solution

The MediaLibrary class is a api to access MediaFiles on the phone (Pictures, Music, ...) you can access the song-collection with the following snippet:

using(MediaLibrary library = new MediaLibrary())
{
    foreach(var song in library.Songs)
    {
        Debug.WriteLine("Name: " + song.Name);
        Debug.WriteLine("Artist: " + song.Artist.Name);
        Debug.WriteLine("Album: " + song.Album.Name);
    }
}

You can also play a song:

MediaPlayer.Play(song);

Reference the Microsoft.Xna.Framework.Media in your Project and make sure you dispose the MediaLibrary after your access.

OTHER TIPS

var library = new MediaLibrary();

1. to get all songs.

foreach (var item in library.Songs)
{
   System.Diagnostics.Debug.WriteLine(item.Album.ToString());
   System.Diagnostics.Debug.WriteLine(item.Artist.Name);
   System.Diagnostics.Debug.WriteLine(item.Duration);
   System.Diagnostics.Debug.WriteLine(item.Name);
   System.Diagnostics.Debug.WriteLine(item.TrackNumber);
}

2. get get all albums.

foreach (var item in library.Albums)
{ 
   System.Diagnostics.Debug.WriteLine("Album ="+ item.Name);
   System.Diagnostics.Debug.WriteLine("Artist = "+item.Artist.Name);
   System.Diagnostics.Debug.WriteLine("TotalSongs ="+ item.Songs.Count);
}

Play song

  1. to play a song

    int index =0;
    MediaPlayer.Play(library.Songs[index]);
    
  2. to play a collection of song

    MediaPlayer.Play(library.Songs);
    
  3. to play a collection of song that start from a specific index. check the song exist in the list

    int index = 5;
    if(index<=library.Songs.Count-1)  
    MediaPlayer.Play(library.Songs, index);
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top