【问题标题】:Embedding Youtube Videos in webbrowser. Object doesn't support property or method在网络浏览器中嵌入 Youtube 视频。对象不支持属性或方法
【发布时间】:2018-02-21 11:02:48
【问题描述】:

Youtube 最近停止支持以 www.youtube.com/v/{key} 格式嵌入的视频。所以我试图将视频从“/v/”转换为“/embed/”。但是,当我尝试导航到视频时,会弹出以下错误:

我正在使用以下内容导航到网页:

WPF

<WebBrowser x:Name="trailer" Margin="655,308,30,135"/>

c#

trailer.Navigate("https://www.youtube.com/embed/v2fDTOdWuQQ?rel=0&amp;showinfo=0");

为什么不能简单地从“/v/”切换到“/embed/”?我该如何解决这个问题?

【问题讨论】:

  • 在导航之前调用它:stackoverflow.com/a/34267121/403671
  • @SimonMourier 我有完全相同的问题,看来 RooiWillie 在您的链接上的回答适用于我的情况。
  • @JohnOdom 是的,这是该主题中的最佳答案,但是,嘿,它不值 500 美元 :-)
  • blog example,你可以使用&lt;iframe width="100%" height="480" src="https://www.youtube.com/embed/0mNptklwylc?rel=0" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;嵌入。它也在示例页面中工作。

标签: c# wpf youtube


【解决方案1】:

尝试将您的 WebBrowser 设置为“静默模式”(忽略这些脚本错误)。 它需要一些黑色的 IE/COM 魔法,但它可以工作。

请参阅https://stackoverflow.com/a/6198700/3629903 了解如何操作。

【讨论】:

  • 静默模式只是忽略错误,留下一个黑框。
【解决方案2】:

这是现有 SO 线程的副本

Use latest version of Internet Explorer in the webbrowser control

线程中有很多答案,并附有实际代码。

最好的建议是在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION 中为您的app.exe 设置一个非常高的数字

我将它设置为20000,假设它可以在许多即将发布的版本中工作并使用最新版本是安全的。这种情况在你的 exe 设置过程中很容易完成。所以你不必担心哪个版本存在,哪个不存在。嵌入工作所需的最低版本是 IE 9。

另外,另一种选择是根本不使用嵌入式 IE。相反,使用铬。在

上有一个相同的 CefSharp 项目

https://cefsharp.github.io/

此项目允许您在 WinForms 或 WPF 应用程序中嵌入 chromium 浏览器。该应用程序非常简单

using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        ChromiumWebBrowser chrome;

        private void InitChrome()
        {
            CefSettings settings = new CefSettings();
            Cef.Initialize(settings);
            chrome = new ChromiumWebBrowser("https://www.youtube.com/embed/v2fDTOdWuQQ?rel=0&amp;showinfo=0");
            this.Controls.Add(chrome);
            chrome.Dock = DockStyle.Fill;
        }
        public Form1()
        {
            InitializeComponent();
            InitChrome();
            //this.webBrowser1.Navigate("https://www.youtube.com/embed/v2fDTOdWuQQ?rel=0&amp;showinfo=0");
        }

    }
}

而且效果很好。这将使您的应用不依赖于目标机器上安装的浏览器。

【讨论】:

    【解决方案3】:

    使用 WebBrowser.NavigateToString 代替 WebBrowser.Navigate 并使用 HTML 代替 URL。 检查此屏幕截图以供您参考

    【讨论】:

    【解决方案4】:

    WPF 具有“浏览器仿真”功能来解决此类问题。

    添加以下枚举

    public enum BrowserEmulationVersion
    {
        Default = 0,
        Version7 = 7000,
        Version8 = 8000,
        Version8Standards = 8888,
        Version9 = 9000,
        Version9Standards = 9999,
        Version10 = 10000,
        Version10Standards = 10001,
        Version11 = 11000,
        Version11Edge = 11001
    }
    

    创建一个新类“InternetExplorerBrowserEmulation”并在其中添加以下代码。

    public class InternetExplorerBrowserEmulation
    {
        private const string InternetExplorerRootKey = @"Software\Microsoft\Internet Explorer";
        private const string BrowserEmulationKey = InternetExplorerRootKey + @"\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
    
    
        public static int GetInternetExplorerMajorVersion()
        {
            int result;
    
            result = 0;
    
            try
            {
                RegistryKey key;
    
                key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey);
    
                if (key != null)
                {
                    object value;
    
                    value = key.GetValue("svcVersion", null) ?? key.GetValue("Version", null);
    
                    if (value != null)
                    {
                        string version;
                        int separator;
    
                        version = value.ToString();
                        separator = version.IndexOf('.');
                        if (separator != -1)
                        {
                            int.TryParse(version.Substring(0, separator), out result);
                        }
                    }
                }
            }
            catch (SecurityException)
            {
                // The user does not have the permissions required to read from the registry key.
            }
            catch (UnauthorizedAccessException)
            {
                // The user does not have the necessary registry rights.
            }
    
            return result;
        }
    
        public static bool SetBrowserEmulationVersion(BrowserEmulationVersion browserEmulationVersion)
        {
            bool result;
    
            result = false;
    
            try
            {
                RegistryKey key;
    
                key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);
    
                if (key != null)
                {
                    string programName;
    
                    programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
    
                    if (browserEmulationVersion != BrowserEmulationVersion.Default)
                    {
                        // if it's a valid value, update or create the value
                        key.SetValue(programName, (int)browserEmulationVersion, RegistryValueKind.DWord);
                    }
                    else
                    {
                        // otherwise, remove the existing value
                        key.DeleteValue(programName, false);
                    }
    
                    result = true;
                }
            }
            catch (SecurityException)
            {
                // The user does not have the permissions required to read from the registry key.
            }
            catch (UnauthorizedAccessException)
            {
                // The user does not have the necessary registry rights.
            }
    
            return result;
        }
    
        public static bool SetBrowserEmulationVersion()
        {
            int ieVersion;
            BrowserEmulationVersion emulationCode;
    
            ieVersion = GetInternetExplorerMajorVersion();
    
            if (ieVersion >= 11)
            {
                emulationCode = BrowserEmulationVersion.Version11;
            }
            else
            {
                switch (ieVersion)
                {
                    case 10:
                        emulationCode = BrowserEmulationVersion.Version10;
                        break;
                    case 9:
                        emulationCode = BrowserEmulationVersion.Version9;
                        break;
                    case 8:
                        emulationCode = BrowserEmulationVersion.Version8;
                        break;
                    default:
                        emulationCode = BrowserEmulationVersion.Version7;
                        break;
                }
            }
    
            return SetBrowserEmulationVersion(emulationCode);
        }
    
        public static BrowserEmulationVersion GetBrowserEmulationVersion()
        {
            BrowserEmulationVersion result;
    
            result = BrowserEmulationVersion.Default;
    
            try
            {
                RegistryKey key;
    
                key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true);
                if (key != null)
                {
                    string programName;
                    object value;
    
                    programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
                    value = key.GetValue(programName, null);
    
                    if (value != null)
                    {
                        result = (BrowserEmulationVersion)Convert.ToInt32(value);
                    }
                }
            }
            catch (SecurityException)
            {
                // The user does not have the permissions required to read from the registry key.
            }
            catch (UnauthorizedAccessException)
            {
                // The user does not have the necessary registry rights.
            }
    
            return result;
        }
    
    
        public static bool IsBrowserEmulationSet()
        {
            return GetBrowserEmulationVersion() != BrowserEmulationVersion.Default;
        }
    }
    

    在设置 URL 之前,我们需要添加以下代码。

    if (!InternetExplorerBrowserEmulation.IsBrowserEmulationSet())
    {
      InternetExplorerBrowserEmulation.SetBrowserEmulationVersion();
    }
    

    这将使用 WPF 中的 webbrowser 控件运行你的视频

    【讨论】:

      猜你喜欢
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-05
      • 2015-04-14
      • 2020-09-03
      相关资源
      最近更新 更多