question

HarvinderSingh-7319 avatar image
0 Votes"
HarvinderSingh-7319 asked ·

How to download and play remote media using MediaPlayer in UWP?



I am trying to play remote media using MediaPlayer and MediaSource created from CreateFromDownloadOperation, with the idea that media would start playing as soon as some meaningful data is downloaded through DownloadOperation. Also, I would be able to save it locally for the next time.

@jadailey
It was mentioned in the forum post sometime back here at https://social.msdn.microsoft.com/Forums/en-US/37b2650c-4d81-4448-91ea-c57be33d4c74/rs41803uwpc-media-player-mediasource-createfromdownloadoperation-deleted-download-files?forum=wpdevelop

Breeze asked me to take a look at this. Is there a particular reason that you are using the background download playback functionality in your app?

The intent of this functionality is to allow for immediate playback of an audio or video file while also caching the file locally for offline playback at a later date.


However, what I am finding is that the media does not start playing till the file is completely downloaded. I am on 1803, with Target set to 1903 and min target of 1803. Here is basics of my code

 var destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
     $"{filePathHash}", CreationCollisionOption.ReplaceExisting);

 var download = new BackgroundDownloader().CreateDownload(url, destinationFile);
 download.IsRandomAccessRequired = true;
    
 var task = Task.Run(async ()=> await download.StartAsync());

 mediaPlayer.Source = MediaSource.CreateFromDownloadOperation(download);    


However if I create mediaSource from MediaSource.CreateFromUri(), it starts playing almost immediately.

File used for testing https://www.thenakedscientists.com/sites/default/files/media/podcasts/episodes/Ask_19.12.06.mp3.

windows-uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered ·

How to download and play remote media using MediaPlayer in UWP?

when you call CreateFromDownloadOperation method, it will start download automatically, you have no need call StartAsync. Please try following code.


 private async void MyMedia_Loaded(object sender, RoutedEventArgs e)
 {
     var destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync($"file.mp3", CreationCollisionOption.ReplaceExisting);
     var url = new Uri("https://www.thenakedscientists.com/sites/default/files/media/podcasts/episodes/Ask_19.12.06.mp3");
     var download = new BackgroundDownloader().CreateDownload(url, destinationFile);
     download.IsRandomAccessRequired = true;     
     MyMedia.Source = MediaSource.CreateFromDownloadOperation(download);      
     MyMedia.AutoPlay = true;
 }


· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Apologies if my title was misleading, what I wanted to add to the title was how to concurrently download and play the media files.

The example works, but the media does not start playing immediately, but only after complete file is downloaded first.

0 Votes 0 ·

it is by-design, it need to cache some data. but it does not waiting for the full file download

0 Votes 0 ·