Description
This tutorial will show how to play a YouTube video directly from an app without opening the browser.
For this tutorial you will first need to include a MyToolkit reference in your project.
You can add the reference using Nuget Package manager. Search for Mytoolkit in the Nuget Package manager.
For a reference on how to use Nuget Package Manager:
Process of Installing the Nuget Package Manager in ASP.NET Web API
Step 1
First we will create the basic page with a button and Button_Click_1 event handler for the button click as in the following:
Code
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Height="100" Width="200" Content="Play!" Click="Button_Click_1" />
</Grid>
Step 2
In the C# code include the namespace "MyToolkit.Multimedia".
Code
using MyToolkit.Multimedia;
Step 3
Use the Play method in the YouTube class to play a video as in the following:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
YouTube.Play("uELoogRgvo4", true, YouTubeQuality.Quality480P, (ex) =>
{
if (ex != null)
{
SystemTray.ProgressIndicator.IsVisible = false;
MessageBox.Show(ex.Message);
}
});
}
The first parameter method is the YouTube video ID that should be passed as the string.
For example, for this YouTube video the underlined part shows the video ID.
https://www.youtube.com/watch?v=uELoogRgvo4
The second parameter is "manualActivatePage" and should be set to true for best performance.
The third parameter is the video quality and the fourth parameter is an action.
Step 4
Override the "OnBackKeyPress" and qOnNavigatedTo methods in the page as in the following:
protected override void OnBackKeyPress(CancelEventArgs e)
{
if (YouTube.CancelPlay()) // used to abort current youtube download
e.Cancel = true;
base.OnBackKeyPress(e);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
YouTube.CancelPlay();// enable page after video playback is completed
base.OnNavigatedTo(e);
}
Reference
MyToolkit