HTTP Live Streaming In Windows 10 UWP

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:

  1. <Grid>  
  2.    <MediaElement x:Name="liveMedia" />  
  3. </Grid>  
Next add two app bar controls to Play and Pause the streaming.
  1. <Page.BottomAppBar>  
  2.    <AppBar IsOpen="True">  
  3.       <StackPanel Orientation="Horizontal">  
  4.          <AppBarButton Name="playBtn" Click="playBtn_Click" Icon="Play" Label="Play"></AppBarButton>  
  5.          <AppBarButton Name="pausBtn" Click="pausBtn_Click" Icon="Pause" Label="Pause"></AppBarButton>  
  6.       </StackPanel>  
  7.    </AppBar>  
  8. </Page.BottomAppBar>  
Next go to code behind page and write the following code to stream the video:
  1. var streamUri = new Uri(""); //replace your URL  
  2. var streamResponse = await AdaptiveMediaSource.CreateFromUriAsync(streamUri);  
  3. if (streamResponse.Status == AdaptiveMediaSourceCreationStatus.Success)  
  4. liveMedia.SetMediaStreamSource(streamResponse.MediaSource);  
  5. else  
  6. {  
  7.    //not found  
  8. }  
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:
  1. private void pausBtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    liveMedia.Pause();  
  4. }  
If you want to play again write the following code:
  1. private void playBtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    liveMedia.Play();  
  4. }  
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.


Similar Articles