Adding a song into music in the Windows Phone Emulator in Visual Studio 2012

前端 未结 1 722
醉梦人生
醉梦人生 2020-12-22 09:52

I am using Visual Studio 2012 and Windows Phone 8.0 SDK.
I want to add a song to Music in the emulator for debugging.

How can I achieve this?

相关标签:
1条回答
  • 2020-12-22 09:58

    Copy a Happy.mp3 file in your project Assets folder Add the file to the project: enter image description hereenter image description here
    and set the Happy.mp3 Properties :

    MP3Properties

    Then run this code to copy the song to the MediaLibrary.

    Uri file = new Uri("Assets/Happy.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);            
    
    0 讨论(0)
提交回复
热议问题