none
MediaLibrary SaveSong method

    Question

  • Hi,

    I tried to use the new WP8 API to save songs into a Lumia 920 device, but i received an InvalidOperationException. I tried to call it like this:

    library.SaveSong(new Uri(“Assets/god_gave_me_you.mp3”, UriKind.Relative), songMetadata, SaveSongOperation.CopyToLibrary);

    This new API is in the Microsoft.Xna.Framework.Media.PhoneExtensions namespace in the assembly Microsoft.Xna.Framework.MediaLibraryExtensions.dll.

    Did anybody used this method or could tell me where i am wrong?

    Any suggestion is appreciated.

    Wednesday, December 19, 2012 12:40 PM

Answers

  • here's the code that worked for me:

    private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                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);            
            }

    • Proposed as answer by kennethkryger79 Tuesday, January 08, 2013 6:10 PM
    • Marked as answer by Dragos Mihai Friday, January 11, 2013 10:38 AM
    Tuesday, January 08, 2013 7:17 AM

All replies

  • I think you have to add the proper capabilities in the app manifest file.  Have you done anything like that?


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, December 19, 2012 4:51 PM
  • You need ID_CAP_MEDIALIB_AUDIO as described in the documentation.
    Thursday, December 20, 2012 12:11 AM
  • Hello,

    It happened I have the same problem. When I try to SaveSong() for file from IsolatedStorage the same exception happens.

    All necessary capabilities are added.

            async void SaveASong()
            {
                byte[] destinationArray;
                StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                StorageFile file = await folder.GetFileAsync("Dive.mp3");
                using (var fs = await file.OpenReadAsync())
                {
                    DataReader reader = new DataReader(fs);
                    destinationArray = new byte[fs.Size];
                    await reader.LoadAsync((uint)fs.Size);
                    reader.ReadBytes(destinationArray);
                }
    
                var myStore = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream ifs = myStore.CreateFile("Dive.mp3");
    
                ifs.Write(destinationArray, 0, destinationArray.Length);
                
                ifs.Close();
    
                MediaLibrary ml = new MediaLibrary();
                SongMetadata metadata = null;
    
                if (!myStore.FileExists("Dive.mp3"))
                {
                    MessageBox.Show("File doesn't exist");
                }
    
                myStore.Dispose();
    
                ml.SaveSong(new Uri("isostore:/Dive.mp3", UriKind.Absolute), metadata, SaveSongOperation.CopyToLibrary);
            }
    Probably I do something wrong :(
    Thursday, December 20, 2012 8:16 AM
  • All necessary capabilities are added.

    Regarding the example provided by Andrey Klebanov, here http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff402541%28v=vs.105%29.aspx there is a note which tells do not use SaveSong method with isostore URI scheme.

    Thursday, December 20, 2012 3:20 PM
  • here's the code that worked for me:

    private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                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);            
            }

    • Proposed as answer by kennethkryger79 Tuesday, January 08, 2013 6:10 PM
    • Marked as answer by Dragos Mihai Friday, January 11, 2013 10:38 AM
    Tuesday, January 08, 2013 7:17 AM
  • Hi,

    you can save songs only from the isolated storage. Also, url to the file should not contain "isostore:", simply type relative path from the iso store.


    Toni Petrina
    My blog: Toni codes .NET
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful"

    Tuesday, January 08, 2013 7:32 AM
  • Hi,

    Is it possible to add Podcats to the "Podcast Collection" of the Music + Videos app?
    Or can you only add songs?

    Tuesday, January 08, 2013 6:18 PM
  • Thanks D.Y.Y, finally a working example. Much obliged since the WP8 emulator is way more complete than the WP7 one, but for some reason lacks any demo music.

    However, keep in mind that it also seem to throw an InvalidOperation when that song with that exact metadata is already present, so only click the button once, catch the exception or randomize the metadata ;).

    Tuesday, January 08, 2013 9:16 PM

  • Or can you only add songs?

    This.

    Tuesday, January 08, 2013 9:27 PM
  •    

     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";
            string u = "/Images/NewSort/HYLX.jpg"; 
           metaData.ArtistBackgroundUri = new Uri(u, UriKind.Relative); I write how not picture it?


    爱编程 爱生活

    Thursday, February 07, 2013 8:12 AM
  • Can you please give an example how to add:

    metaData.Duration

    to insure having the song duration reported correctly

    Thank you


    Eng. Ash

    Friday, February 07, 2014 3:53 AM
  • how to add metaData.Duration ?

    Regards


    Eng. Ash

    Friday, February 07, 2014 3:54 AM
  • Did this method work?
    I tried to add a BackgroundUri, unfortunately it didnt work neither from isolatedstorage nor from Project?

    What's wrong ?

     SongMetadata smd = new SongMetadata()
                        {
                            AlbumName = "aName"
                            AlbumArtistName = "abName"
                            ArtistName = "arName"
                            GenreName = "genre"
                            Name = "name"
                            AlbumArtistBackgroundUri = new Uri("Image/bg.jpg", UriKind.RelativeOrAbsolute), //from project
                            AlbumArtUri = new Uri("bg.jpg", UriKind.RelativeOrAbsolute),  //from isolated
                        };

    Tuesday, March 18, 2014 4:25 PM