Search code examples
c#windows-phone-8isolatedstorage

Unable to download audio with MediaLibraryExtensions.SaveSong in windows phone 8


I am trying to download an audio and saving to isolated storage and saving to emulator.

I tried with the following code.

WebClient m_webClient = new WebClient();
m_webClient.OpenReadAsync(fileUri);
m_webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_ImageOpenReadCompleted);
m_webClient.AllowReadStreamBuffering = true;  


private static void webClient_ImageOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
        var isolatedfile = IsolatedStorageFile.GetUserStoreForApplication();
        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(_fileName, System.IO.FileMode.Create, isolatedfile))
        {
            byte[] buffer = new byte[e.Result.Length];
            while (e.Result.Read(buffer, 0, buffer.Length) > 0)
            {
                stream.Write(buffer, 0, buffer.Length);
            }
        }
        SaveFileMP3(_fileName);
}

private static void SaveFileMP3(string _fileName)
{
        MediaLibrary lib = new MediaLibrary();
        Uri songUri = new Uri(_fileName, UriKind.RelativeOrAbsolute);
        MediaLibraryExtensions.SaveSong(lib, songUri, null, SaveSongOperation.CopyToLibrary);

}

The problem I am facing is, the audio getting saved to the emulator but not with the extension. Suppose the file name "test.MP3", it is saved as "test" and the duration is always 0:0:00 irrespective to the original duration.

I have added Audio capability too in the manifest file.

Following is the screenshot while saving the song, which has many exceptions in it.

enter image description here

Please correct if something is wrong with code. Thanks in advance.


Solution

  • By adding the line of lib.savesong worked perfectly instead of using mediaLibraryExtensions.

    private static void SaveFileMP3(string _fileName)
    {
        MediaLibrary lib = new MediaLibrary();
        Uri songUri = new Uri(_fileName, UriKind.RelativeOrAbsolute);
        lib.SaveSong(songUri, null, SaveSongOperation.CopyToLibrary);
        //MediaLibraryExtensions.SaveSong(lib, songUri, null, SaveSongOperation.CopyToLibrary);
    
    }