Reputation: 1180
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?
Upvotes: 0
Views: 1435
Reputation: 6178
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
to play a song
int index =0;
MediaPlayer.Play(library.Songs[index]);
to play a collection of song
MediaPlayer.Play(library.Songs);
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);
Upvotes: 0
Reputation: 2706
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.
Upvotes: 5