2012年10月31日 星期三

Playing youtube in full screen in WebBrowser control


I try to play youtube video. This is what I have tried:
this.webBrowser1.Source = new Uri("http://youtube.googleapis.com/v/L8bE5-g8VC0");
This one displays YouTube player with all player controls. However full screen button doesn't work. I click on it, but player doesn't go full screen. Button becomes just disabled.
I have also tried this one:
this.webBrowser1.Source = new Uri("http://www.youtube.com/embed/L8bE5-g8VC0");
This also display YouTube player with all player controls. Full screen button is working properly. However when I go again to this video or another one (by setting Source property), player buttons disappear. To see player buttons again, I need to delete temporary internet files for IE. I could delete temp files every time before playing video, but this is not solution for me.
I'm running Windows 7 64bit and using WPF 4.0. What I want is to display YouTube player in my WebBrowser and have full screen button working properly. Anyone have some idea?



Solution, which worked for me - building a small HTML page with embedded 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));
    }
}
Usage:
this.webBrowser1.ShowYouTubeVideo("L8bE5-g8VC0");

沒有留言:

張貼留言