0
votes

I'm having a play with writing a mp3 player app for WP8, using MediaLibrary to handle the phone's own mp3 collection. I want to test the result in the phone emulator on VS2013, but when I use the following code:

using (MediaLibrary library = new MediaLibrary())
        {
            SongCollection songs = library.Songs;
            Song song = songs[0];
            MediaPlayer.Play(song);
        }

The song collection is empty, presumably because VS doesn't have any knowledge of a media library with songs in.

Is there any way to test this in the emulator using a fake medialibrary or for VS to use windows' media library? I just want to see (or hear) the code working before I proceed :)

2

2 Answers

1
votes

I have managed to find a workaround!

If you add an mp3 file to the app's assets, the following code will add the mp3 to the media player library:

private void AddSong()
    {
        Uri file = new Uri("Assets/someSong.mp3", UriKind.Relative);

        //copy file to isolated storage
        var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
        var fileStream = myIsolatedStorage.CreateFile("someSong.mp3");
        var resource = Application.GetResourceStream(file);
        int chunkSize = 4096;
        byte[] bytes = new byte[chunkSize];
        int byteCount;
        while ((byteCount = resource.Stream.Read(bytes, 0, chunkSize)) > 0)
        {
            fileStream.Write(bytes, 0, byteCount);
        }
        fileStream.Close();

        Microsoft.Xna.Framework.Media.PhoneExtensions.SongMetadata metaData = new Microsoft.Xna.Framework.Media.PhoneExtensions.SongMetadata();
        metaData.AlbumName = "Some Album name";
        metaData.ArtistName = "Some Artist Name";
        metaData.GenreName = "test";
        metaData.Name = "someSongName";

        var ml = new MediaLibrary();
        Uri songUri = new Uri("someSong.mp3", UriKind.RelativeOrAbsolute);
        var song = Microsoft.Xna.Framework.Media.PhoneExtensions.MediaLibraryExtensions.SaveSong(ml, songUri, metaData, Microsoft.Xna.Framework.Media.PhoneExtensions.SaveSongOperation.CopyToLibrary);            
    }

I also needed to add:

using System.IO.IsolatedStorage;

I would love to claim credit for this, but I found the answer here:

http://social.msdn.microsoft.com/forums/wpapps/en-US/f5fa73da-176b-4aaa-8960-8f704236bda5/medialibrary-savesong-method

0
votes

By default the media library on the emulator is empty. I also do not think it is possible to automagically hook up your dev machine's music folder to the emulator to test that way. It might be possible to manually configure the emulated phone with an email account! and save music onto it that way, but even if that worked you'd have to do it each and every time you restart the emulator.

Best way to test would be t deploy to a real device.