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.

Comments

  • Anonymous
    September 07, 2013
    Hi, I was looking for this solution! Very very thanks:)

  • Anonymous
    October 14, 2013
    I used a library i found on codeplex called mytoolkit.  It is also available as a nuget package which is how i ended up installing it.  You pass in the videoid and it returns you the url of the mp4 video which i then play on with the mediaplayer framework.  Of course this violates youtube's terms of service.  the project can be found at this url http://mytoolkit.codeplex.com

  • Anonymous
    October 26, 2013
    i have error in videoID the error be . (the name 'videoID' does not exist in the current context)

  • Anonymous
    April 01, 2014
    @Mohamed , u must create a String like this "String videoID="TheVlueOfYouVideoID" ;

  • Anonymous
    May 07, 2014
    Not for a protected video ! how do for protected video ?