YouTube class

Package: MyToolkit.Extended
Platforms: WP7SL, WP8SL, WinRT(Universal), WPF

Windows Phone Silverlight

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
		SystemTray.ProgressIndicator.IsVisible = false; 

		// your code here

		base.OnNavigatedTo(e);
	}

	private async void OnButtonClick(object o, RoutedEventArg e)
	{
		SystemTray.ProgressIndicator.IsVisible = true; 
		try
		{
			await YouTube.PlayWithPageDeactivationAsync(
				"Zln9I9IttLA", true, YouTubeQuality.Quality480P);
		}
		catch (Exception ex)
		{
			SystemTray.ProgressIndicator.IsVisible = false; 
			MessageBox.Show(ex.Message);
		}
	}
}

When using ExtendedPage from MyToolkit, you cannot override the OnKeyBackPress method. Instead you have to register a key back handler:

public partial class MyPage : PhoneApplicationPage
{
	public MyPage()
	{
		...
		AddBackKeyPressedHandler(YouTube.HandleBackKeyPress);
	}

	...
}

WinRT (Universal)

For Universal Windows and Windows Phone apps

Play with MediaElement

To play a YouTube video using a MediaElement, use the GetVideoUriAsync method:

The XAML with the MediaElement:

<MediaElement x:Name="player" Width="480" Height="320" />

The code to start the video in the player (for example called in a button clicked handler):

var url = await YouTube.GetVideoUriAsync(youTubeId, YouTubeQuality.Quality720P);
if (url != null)
{
	player.Source = url.Uri;
	player.Play();
}
else
	// TODO show error (video uri not found)

To avoid buffering issues in low bandwidth scenarios it is recommended to use the MS player framework (see http://mytoolkit.codeplex.com/discussions/433193)

Play in external player

TODO: Find out how to do replace the Windows Phone's MediaPlayerLauncher class to implement YouTube.PlayAsync. See http://stackoverflow.com/questions/24686773/replacement-of-mediaplayerlauncher-in-windows-phone-8-1-universal-apps

Solution may look like this but the current code launches the browser instead of video player (when not using the options).

var youTubeId = "yourYouTubeId";
try
{
    //// TODO: Show progress bar
    var uri = await YouTube.GetVideoUriAsync(youTubeId, YouTubeQuality.Quality1080P);
    if (uri != null)
    {
        var options = new LauncherOptions();
        options.ContentType = "video/mp4";
        await Launcher.LaunchUriAsync(uri.Uri, options); 
    }
    else
        throw new Exception("no_video_urls_found");
}
catch (Exception exception)
{
    //// TODO: Show exception
}
finally
{
    //// TODO: Hide progress bar
}

YouTube Terms of Service

Be aware that the YouTubes Terms of Service states in part II, 10. that it is prohibited to "access any portion of any YouTube audiovisual content by any means other than use of a YouTube player or other video player expressly authorized by YouTube".

https://developers.google.com/youtube/terms

Last edited Jul 16 at 10:04 PM by rsuter, version 73

Comments

jKy May 10 at 11:35 PM 
@ababil10, I was having the same issue as you, but then I released YouTube id is case sensative

ababil10 Apr 25 at 12:12 PM 
This class not working in windows phone 8.1.
Please suggest me or give sample code to achieve this functionality in WP 8.1.
Thanks in advance.

Khanal Apr 18 at 6:39 AM 
Great stuff thanks you so much, but I would like to know is there any such library for winjs :D

Pluginbaby Mar 25 at 2:36 PM 
Hi,
Does that help ?
http://googledevelopers.blogspot.co.uk/2014/03/ga-release-for-google-apis-client.html?m=1
New Youtube API for .NET

Apollon513 Sep 9, 2013 at 7:07 AM 
Do the YouTube Terms of Service say that it is not allowed to use this framework and show the videos in the MS Media Player Framework?

Mikon Jun 3, 2013 at 2:38 PM 
can anyone be more specific on how to use this brilliant library for beginner programmers?!! Thank you in advance!

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

Saswati Feb 12, 2013 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