YouTube class

YouTube.Play will download the YouTube page of the required YouTube-ID, search for the appropriate MP4-link and then play the video with the MediaPlayerLauncher. It's a better method than using the WebBrowserTask and browse to a YouTube-URL (eg "vnd.youtube:YOUTUBEID?vndapp=youtube_mobile")

The simplest call is YouTube.Play("youtubeid"). This does not need any changes in the page code. To deactivate the current page until the MP4-link has been found, use YouTube.Play("youtubeid", false).

There are two problems with the previous method. First it does not allow the user to cancel the download of the MP4 link (you have to extend the page's OnBackKeyPress method). The second problem is that after downloading the video MP4 link you will notice that the page will be active until the MediaPlayerLauncher has started. To solve both problems use YouTube.Play("youtubeid", true) and add the following code in OnNavigatedTo and OnBackKeyPress. This is the best but most "complex" solution:

public partial class MyPage : PhoneApplicationPage
{
	protected override void OnBackKeyPress(CancelEventArgs e)
	{
		if (YouTube.CancelPlay()) // used to abort current youtube download
			e.Cancel = true;
		else
		{
			// your code here
		}
		base.OnBackKeyPress(e);
	}

	protected override void OnNavigatedTo(NavigationEventArgs e)
	{
		YouTube.CancelPlay(); // used to reenable page
		// your code here
		base.OnNavigatedTo(e);
	}

	private void OnButtonClick(object o, RoutedEventArg e)
	{
		YouTube.Play("youtube_id", true, YouTubeQuality.Quality480P, (e) => { if (x != null) { MessageBox.Show(x.Message); } });
	}
}

Eventually you should show the progress bar while downloading the MP4 link:

	protected override void OnNavigatedTo(NavigationEventArgs e)
	{
		YouTube.CancelPlay(); // used to reenable page
		SystemTray.ProgressIndicator.IsVisible = false; 
		// your code here
		base.OnNavigatedTo(e);
	}

	private void OnButtonClick(object o, RoutedEventArg e)
	{
		SystemTray.ProgressIndicator.IsVisible = true; 
		YouTube.Play("youtube_id", true, YouTubeQuality.Quality480P, x =>
		{
			if (x != null)
				MessageBox.Show(x.Message);
		});
	}
}

WinRT and MediaElement

To play a YouTube video in WinRT MediaElement, use the GetVideoUriAsync:

var url = await YouTube.GetVideoUriAsync(youTubeId, YouTubeQuality.Quality1080P);
var player = new MediaElement();
player.Source = url.Uri;

Classes

Last edited Oct 4, 2012 at 10:32 PM by rsuter, version 30

Comments

kvbhaskar7 Apr 8 at 7:14 AM 
Any librabry available using winJS?

Saswati Feb 12 at 6:36 AM 
I am trying to play YouTube video in my WinRT applicatiob by using the above sample. But I am getting the following exception.
"An exception of type 'MyToolkit.Networking.HttpStatusException' occurred in mscorlib.dll but was not handled in user code"
I am actually adding this code inside the Grid Loaded event. Please let me know what I am missing here.

cristianomad Nov 6, 2012 at 5:51 PM 
Thanks man! You save my day!

bennyn Nov 3, 2012 at 1:04 AM 
Awesome library! I use it to embed YouTube videos in my Windows 8 app. Thank you very much.

dtentori Oct 4, 2012 at 8:11 PM 
great job! thanks!

joanromano May 1, 2012 at 5:22 PM 
Amazing stuff man, works great! Thanks so much :D