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 11:04 PM by rsuter, version 73