Search code examples
c#windows-phone-8.1mp3mediadownload

Downloading song files in windows phone 8.1 C#


I am building an app, that needs to download mp3 files and storage in app folder. My download button is running the structions below.

Uri source = new Uri(url);
string destination = "\\mp3";

StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
destination, CreationCollisionOption.GenerateUniqueName);

BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source, destinationFile);

Now i am stopped here, how can i continue with the download file, what method or class i need to continue. I searched about "MediaLibraryExtensions.SaveSong Method" but is this for windows phone 8 and 7.1.


Solution

  • First, you need to define correct file name in "destination" variable. Next, you are saving file in wrong folder for music, the song should be in "KnownFolders.MusicLibrary". If you want to download into application local folder, use ApplicantionData.Current.LocalFolder(). Then, use the below code to start downloading (progressChanged method is not written here, it is used to keep track of download process, but you can easily define it yourself):

    Progress<DownloadOperation> progress = new Progress<DownloadOperation>(progressChanged);
    cancellationToken = new CancellationTokenSource();
    await download.StartAsync().AsTask(cancellationToken.Token, progress);