Play youtube in full screen in a WebBrowser control

I have XAML:

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <WebBrowser x:Name="webBrowser1"/> </Grid> 

I am trying to play a video on YouTube. This is what I tried:

 this.webBrowser1.Source = new Uri("http://youtube.googleapis.com/v/L8bE5-g8VC0"); 

This window displays the YouTube player with all player controls. However, the full screen button does not work. I click on it, but the player does not go into full screen mode. The button just turns off.

I also tried this:

 this.webBrowser1.Source = new Uri("http://www.youtube.com/embed/L8bE5-g8VC0"); 

It also shows the YouTube player with all player controls. The fullscreen button works correctly. However, when I again go to this video or another (by setting the Source property), the player buttons disappear. To see the player buttons again, I need to delete temporary Internet files for IE. I can delete temporary files every time before playing the video, but this is not a solution for me.

I am running Windows 7 64bit and using WPF 4.0. I want to display the YouTube player in my WebBrowser and work with the full screen button. Does anyone have an idea?

+4
source share
1 answer

The solution that worked for me was to create a small HTML page with a built-in video player:

 public static class WebBrowserExtensions { private static string GetYouTubeVideoPlayerHTML(string videoCode) { var sb = new StringBuilder(); const string YOUTUBE_URL = @"http://www.youtube.com/v/"; sb.Append("<html>"); sb.Append(" <head>"); sb.Append(" <meta name=\"viewport\" content=\"width=device-width; height=device-height;\">"); sb.Append(" </head>"); sb.Append(" <body marginheight=\"0\" marginwidth=\"0\" leftmargin=\"0\" topmargin=\"0\" style=\"overflow-y: hidden\">"); sb.Append(" <object width=\"100%\" height=\"100%\">"); sb.Append(" <param name=\"movie\" value=\"" + YOUTUBE_URL + videoCode + "?version=3&amp;rel=0\" />"); sb.Append(" <param name=\"allowFullScreen\" value=\"true\" />"); sb.Append(" <param name=\"allowscriptaccess\" value=\"always\" />"); sb.Append(" <embed src=\"" + YOUTUBE_URL + videoCode + "?version=3&amp;rel=0\" type=\"application/x-shockwave-flash\""); sb.Append(" width=\"100%\" height=\"100%\" allowscriptaccess=\"always\" allowfullscreen=\"true\" />"); sb.Append(" </object>"); sb.Append(" </body>"); sb.Append("</html>"); return sb.ToString(); } public static void ShowYouTubeVideo(this WebBrowser webBrowser, string videoCode) { if(webBrowser == null) throw new ArgumentNullException("webBrowser"); webBrowser.NavigateToString(GetYouTubeVideoPlayerHTML(videoCode)); } } 

Using:

 this.webBrowser1.ShowYouTubeVideo("L8bE5-g8VC0"); 
+8
source

All Articles