Good day, I find myself trying to play a youtube video in visual studio c#, honestly I have been all day and I have only found very old information (2013 backwards and many things that do not work), the first is using the Mytoolkit library together with the "MediaElement" component and using the following code
private async void button1_Click(object sender, EventArgs e)
{
Uri _videoUri = await GetYoutubeUri("OgO4v6W72YY");
if (_videoUri != null)
{
player.Source = _videoUri;
player.Play();
}
}
internal async Task<Uri> GetYoutubeUri(string VideoID)
{
YouTubeUri uri = await YouTube.GetVideoUriAsync(VideoID, YouTubeQuality.Quality1080P);
return uri.Uri;
}
And this is the .xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<MediaElement x:Name="Reproductor" LoadedBehavior="Manual" ></MediaElement>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="419,275,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
That is supposed to be enough but it gives me the following error
From what I understand, this error is usually due to the fact that there is no reference to an object, but I don't think that's it, since when you sent a url of a different video to one of Youtube, it is played without problems, example:
private async void button_Click(object sender, RoutedEventArgs e)
{
Uri _videoUri = await GetYoutubeUri("OgO4v6W72YY");
if (_videoUri != null)
{
/*Uri directa*/
Reproductor.Source = new Uri("http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4");
//Reproductor.Source = _videoUri;
Reproductor.Play();
}
}
internal async Task<Uri> GetYoutubeUri(string VideoID)
{
YouTubeUri uri = await YouTube.GetVideoUriAsync(VideoID, YouTubeQuality.Quality1080P);
return uri.Uri;
}
I hope you can help me with this dilemma, thanks
You probably won't like the answer I'm going to give you, but it is what it is.
The problem is not with your code, but that it
MediaElement
has problems playing media on secure servers (https
). If you look at the example that you put that works, the link is abouthttp
.Apparently there was a bug report (which has disappeared, or I can't find it) on the subject that was closed indicating that it was not going to be resolved.
The only solution if you want to continue using
MediaElement
it is to download the video to local and play it from there. Here is an example:PS The code of the youtube video that you put as an example (
OgO4v6W72YY
) returnsforbidden
, it is possible that it is from VeVo or with some reproduction restriction.PD2 In my experience, trying to play YouTube videos externally to their own player just leads to frustration. Google tries to avoid it in every way, and every so often they change things so that what used to work for you no longer does. They have many restrictions by country, device, etc... that will make your life very difficult.