kiewic / MediaPlayerElementWithHttpClient

Use Windows.UI.Xaml.Controls.MediaPlayerElement with Windows.Web.Http.HttpClient.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MediaPlayerElementWithHttpClient

TL;DR

Use Windows.UI.Xaml.Controls.MediaPlayerElement with Windows.Web.Http.HttpClient.

Docs

MediaPlayerElement is a control to play audio and video in Windows Universal/Store apps.

However, when the audio or video is served from an internet server, using HttpClient is a better choice to handle the request because you can provide:

  • Authentication credentials.
  • Custom headers.
  • Etc.

The HttpRandomAccessStream class is a wrapper on top of HttpClient that can stream content from the internet and can be consumed as an IRandomAcessStream or IRandomAccessStreamWithContentType.

Example

MediaPlayerElement mediaPlayerElement = new MediaPlayerElement();

HttpClient client = new HttpClient();

// Add custom headers, credentials, etc.
client.DefaultRequestHeaders.Add("Foo", "Bar");

Uri uri = new Uri("http://video.ch9.ms/ch9/70cc/83e17e76-8be8-441b-b469-87cf0e6a70cc/ASPNETwithScottHunter_high.mp4");

HttpRandomAccessStream stream = await HttpRandomAccessStream.CreateAsync(client, uri);

mediaPlayerElement.SetMediaPlayer(new MediaPlayer());
mediaPlayerElement.Source = MediaSource.CreateFromStream(stream, stream.ContentType);

Note: The server must support HTTP Range headers.

Feedback

Please give it a try and provide feedback.

About

Use Windows.UI.Xaml.Controls.MediaPlayerElement with Windows.Web.Http.HttpClient.

License:MIT License


Languages

Language:C# 100.0%