Software solutions for a mobile world

I recently updated a project from Windows Phone Silverlight to Windows RT. (The next step is to make it a UWP, but one step at a time.)

The app retrieves the users music files from the folder, and allows one to be played as an alarm in my Where's My Phone app.

As we all now know, the method of retrieving/playing files from the libraries changed in RT. To get a list of music files, you do similar to:-

var AllStorageFiles = await      KnownFolders.MusicLibrary.GetFilesAsync(CommonFileQuery.OrderByName);

foreach (var song in AllStorageFiles)
{
   Debug.WriteLine("Song name is: " + song.Name); 
}

This will give you a list of music files, each with a number of properties:-

?song 
{Windows.Storage.StorageFile}
Attributes: Normal ContentType: "audio/mpeg"
DateCreated: {11/30/2015 11:40:28 AM +00:00}
DisplayName: "106 - I'll Turn To Stone - Four Tops" DisplayType: "MP3 File" FileType: ".mp3" FolderRelativeId: "A1B7B5E156B4F681\\106 - I'll Turn To Stone - Four Tops.mp3"
IsAvailable: true
Name: "106 - I'll Turn To Stone - Four Tops.mp3"
Path: "C:\Data\USERS\Public\Music\A complete introduction of northern soul\106 - I'll Turn To Stone - Four Tops.mp3"
Properties: {Windows.Storage.FileProperties.StorageItemContentProperties}
Provider: {Windows.Storage.StorageProvider}
Native View: To inspect the native object, enable native code debugging. 

I display the name in a ListView, and for each song stash away the path, so that when the user chooses one to play, I can retrieve the full details, and play the song. Simple - really. 

The code to play it, using a MediaElement is:- 

var audioFile = await KnownFolders.MusicLibrary.GetFileAsync(songIwantToPlay);
var stream = await audioFile.OpenAsync(FileAccessMode.ReadWrite); mediaPlayer.SetSource(stream, audioFile.ContentType);
mediaPlayer.AutoPlay = true;
mediaPlayer.Play();


This information turns up in all searches for play music from a library on Windows Phone 8.1 RT. The missing step always seems to be, what do I put in 'songIwantToPlay'? From the properties above, I tried both the path, and the name, neither of which played the song, and gave different errors.

The trick is to use the path, minus the 'music' folder.

This can be C:\Data\USERS\Public\Music, D:\Data\USERS\Public\Music, C:\Music\ or D:\Music, depending on if your music collection lives on the device, or on the SD card (more of this later).

In the above case, 'songIwantToPlay' would be set to "A complete introduction of northern soul\106 - I'll Turn To Stone - Four Tops.mp3", and this should play.

This applies to both the device, and the emulator. If you want to know how to put music on your emulator, then look no further than this blog post.

Contact

M: +44 (0)7860 907493

pete@gui-innovations.com

More

Connections

   

Twitter Feed