Share via


Playing Youtube video in a Windows 8 Application

Recently, I was working on a C# Windows 8 application that was required to play Youtube videos as one of the features. At first I wasn’t sure if there was a simple way to accomplish the task, but with a little research I found a very nice solution.

 

First I created a very simple WebView in my *.xaml page:

    1: <WebView x:Name="webView" />

 

You’ll notice I haven’t specified any dimensions or any other attributes other than a name. I’ll use the name in my *.xaml.cs file to reference the WebView instance.

 

Now in my *.xaml.cs file, I simply set the contents of the WebView to an iframe that contains the embedded youtube video. The size of the iframe will define the size of my WebView:

    1: string html = @"<iframe width=""800"" height=""480"" src=""https://www.youtube.com/embed/" + videoID + @"?rel=0"" frameborder=""0"" allowfullscreen></iframe>";
    2:  
    3: this.webView.NavigateToString(html);

 

videoID is a string containing the ID of the video you want. It’s the string that comes after the v=   and before the & in the video URL:

 

image

 

The result is a youtube video embedded in your application with the familiar controls:

image

 

Enjoy.