Microsoft finally released live streaming API supports in UWP Windows 10. Before that we need third party API Windows Phone Streaming Media Assistance. But it will arrive in UWP.
Windows.Media.Streaming.Adaptive namespaceļ¼It helps to play over live http media streaming.
This namespace definition support many kinds of different adaptive streaming protocols, such as Http Live Streaming or HLS is a protocol for streaming media content that is segmented into several small files, each of them in different sizes and qualities to enable the content disseminating an adaptive rate depending on the quality of the connection.
This returns AdaptiveMediaSourceCreationResult which includes the source and whether the manifest was able to be downloaded or what error occurred. Remember that you enabled the InternetClient capability in your appxmanifest file.
Now see the steps how it works.
Create new Windows 10 project and go to MainPage.xaml page design view to design.
Add Media control to play the streamed videos and use the following code to design media control:
- <Grid>
- <MediaElement x:Name="liveMedia" />
- </Grid>
Next add two app bar controls to Play and Pause the streaming.
- <Page.BottomAppBar>
- <AppBar IsOpen="True">
- <StackPanel Orientation="Horizontal">
- <AppBarButton Name="playBtn" Click="playBtn_Click" Icon="Play" Label="Play"></AppBarButton>
- <AppBarButton Name="pausBtn" Click="pausBtn_Click" Icon="Pause" Label="Pause"></AppBarButton>
- </StackPanel>
- </AppBar>
- </Page.BottomAppBar>
Next go to code behind page and write the following code to stream the video:
- var streamUri = new Uri("");
- var streamResponse = await AdaptiveMediaSource.CreateFromUriAsync(streamUri);
- if (streamResponse.Status == AdaptiveMediaSourceCreationStatus.Success)
- liveMedia.SetMediaStreamSource(streamResponse.MediaSource);
- else
- {
-
- }
Before set the media source check the status property to verify that everything went correct otherwise skip it.
By default it will start playing the stream. If you want to pause write the following code:
- private void pausBtn_Click(object sender, RoutedEventArgs e)
- {
- liveMedia.Pause();
- }
If you want to play again write the following code:
- private void playBtn_Click(object sender, RoutedEventArgs e)
- {
- liveMedia.Play();
- }
You can play around the media control like the following function: pause, play, mute, unmute and volume increase etc.
Now run the app and check the output like the following video. Here I am going to stream the live news channel.