在WebBrowser控件中全屏播放youtube [英] Playing youtube in full screen in WebBrowser control

查看:228
本文介绍了在WebBrowser控件中全屏播放youtube的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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>

我尝试播放youtube视频。这是我尝试过的:

I try to play youtube video. This is what I have tried:

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

此页面显示具有所有播放器控件的YouTube播放器。但是,全屏按钮不起作用。我单击它,但播放器无法全屏显示。按钮变为禁用状态。

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.

我也尝试过以下操作:

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

这也会显示具有所有播放器控件的YouTube播放器。全屏按钮正常工作。但是,当我再次转到该视频或另一视频时(通过设置Source属性),播放器按钮会消失。要再次查看播放器按钮,我需要删除IE的临时Internet文件。我可以在播放视频之前每次删除临时文件,但这对我来说不是解决方案。

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.

我正在运行Windows 7 64位并使用WPF 4.0。我想要在我的WebBrowser中显示YouTube播放器,并使全屏按钮正常工作。有人知道吗?

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?

推荐答案

对我有用的解决方案-用嵌入式视频播放器构建一个小的HTML页面:

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));
    }
}

用法:

this.webBrowser1.ShowYouTubeVideo("L8bE5-g8VC0");

这篇关于在WebBrowser控件中全屏播放youtube的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆