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&showinfo=0");
为什么这不能简单地从"/v/“切换到"/embed/"?我该如何解决这个问题呢?
发布于 2017-11-02 16:34:43
这是现有销售订单线程的副本
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的过程中完成。所以你不必担心哪个版本存在,哪个版本不存在,你需要的最低版本是IE9。
另外,另一种选择是根本不使用嵌入式IE。取而代之的是铬。上有一个与此相同的CefSharp项目
此项目允许您在您的WinForms或WPF应用程序中嵌入铬浏览器。这个应用程序非常简单
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&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&showinfo=0");
}
}
}
而且效果很好。这将使您的应用程序不依赖于目标计算机上安装的浏览器。
发布于 2018-02-15 11:51:52
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中的WPF浏览器控件运行you tube视频
发布于 2017-09-13 06:34:24
尝试将WebBrowser设置为“静默模式”(忽略这些脚本错误)。它需要一些黑色的IE/COM魔法,但它是有效的。
有关如何操作的信息,请参阅https://stackoverflow.com/a/6198700/3629903。
https://stackoverflow.com/questions/46185600
复制相似问题