Skip to content

YouTube

Rico Suter edited this page Jul 8, 2015 · 7 revisions
  • Package: MyToolkit.Extended
  • Platforms: WP7SL, WP8SL, WinRT, UWP, WPF

Provides methods to load the MP4 URL of a YouTube video.

try
{
	var url = await YouTube.GetVideoUriAsync(
            youTubeId, YouTubeQuality.Quality720P);
	...
}
catch (Exception exception)
{
	// TODO show error (video uri not found)	
}

WinRT and UWP

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):

try
{
	var url = await YouTube.GetVideoUriAsync(
            youTubeId, YouTubeQuality.Quality720P);

	player.Source = url.Uri;
	player.Play();
}
catch (Exception exception)
{
	// TODO show error (video uri not found)	
}

To avoid buffering issues in low bandwidth scenarios it is recommended to use the MS player framework (also check out this discussion)

Play in external player

TODO: Find out how to do replace the Windows Phone's MediaPlayerLauncher class to implement YouTube.PlayAsync(). Check out this StackOverflow question.

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);

    var options = new LauncherOptions();
    options.ContentType = "video/mp4";
    await Launcher.LaunchUriAsync(uri.Uri, options); 
}
catch (Exception exception)
{
    //// TODO: Show exception
}
finally
{
    //// TODO: Hide progress bar
}

Windows Phone Silverlight

The YouTube.Play() method will download the YouTube page of the required YouTube movie ID, search for the appropriate MP4 link and then play the video using the MediaPlayerLauncher. It is 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("YouTube_Movie_Id"). 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("YouTube_Movie_Id", 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("YouTube_Movie_Id", 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);
	}
	
	...
}

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".

developers.google.com/youtube/terms