Reputation: 5495
I am trying to find a resource that would explain how to retrieve a YouTube video URL to play in an external player. Of course, I have taken a look at the YouTube Data API, but there doesn't seem to be a way to do this.
However, I know it is possible, given that there exist YouTube client applications that are able to stream video (MetroTube is one example).
I saw the related post (How to play a YouTube video in WPF using C#), but it was posted a long time ago. I would like to avoid having to use a WebBrowser control (or WebView in UWP).
Can someone suggest an approach or point to a resource that allows this?
Upvotes: 2
Views: 3433
Reputation: 3923
I have used MyToolKit.Extended
in the past to integrate Youtube
Videos in one of my App.
Install Nuget Package from Here
Once done, you need to call this method
internal async Task<Uri> GetYoutubeUri(string VideoID)
{
YouTubeUri uri = await YouTube.GetVideoUriAsync(VideoID, YouTubeQuality.Quality1080P);
return uri.Uri;
}
Make sure you are passing only VideoID
. i.e, If your URL is
https://www.youtube.com/watch?v=UO-8CMdeSHA
you need to pass only UO-8CMdeSHA
.
Once done, you will receive the actual Media Uri. You can now set this as a source to MediaPlayer. Something like below.
Uri _videoUri = await GetYoutubeUri("UO-8CMdeSHA");
if (_videoUri != null)
{
player.Source = _videoUri;
player.Play();
}
To ensure you see basic Play/Pause button and few others, Use Below in your XAML.
MediaElement x:Name="player" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AreTransportControlsEnabled="True" />
And you should be able to play YouTube
Videos.
Good Luck.
Upvotes: 3