Youtube嵌入视频在Winforms
本文关键字:Winforms 视频 Youtube | 更新日期: 2023-09-27 18:14:21
我在VisualStudio中有一个c# Forms项目,我想在其中嵌入一个youtube视频
为此,我在设计器中添加了一个web浏览器控件。
除了嵌入的youtube视频,浏览器可以正常运行我给他的每个URL 。
而不是视频,我得到一个黑屏,没有声音和"脚本错误"给我以下url:(https://s.ytimg.com/yts/jsbin/www-embed-player-new-vflQo6seZ/www-embed-player-new.js)
在正常的youtube中,web浏览器工作很好 -就像chrome一样。
youtube.com/embed/…
我认为这不是HTML5的问题。
我该如何解决这个问题?
是否有其他网页浏览器我可以使用在Winforms?
重现错误的代码:(in Form1 constructor)
WebBrowser youtubePlayer = new WebBrowser();
this.Controls.Add(youtubePlayer);
youtubePlayer.Navigate("http://www.youtube.com/embed/M7lc1UVf-VE");
由于另一个答案已被弃用,因此您现在应该这样做:
WebBrowser控件(包括WPF和WinForms版本)的行为在许多方面与完整的IE不同。您可能希望实现Feature Control以使其行为尽可能接近IE(特别是FEATURE_BROWSER_EMULATION)。下面是一些代码:
private void SetBrowserFeatureControlKey(string feature, string appName, uint value)
{
using (var key = Registry.CurrentUser.CreateSubKey(
String.Concat(@"Software'Microsoft'Internet Explorer'Main'FeatureControl'", feature),
RegistryKeyPermissionCheck.ReadWriteSubTree))
{
key.SetValue(appName, (UInt32)value, RegistryValueKind.DWord);
}
}
例如:private void SetBrowserFeatureControl()
{
// http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx
// FeatureControl settings are per-process
var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
// make the control is not running inside Visual Studio Designer
if (String.Compare(fileName, "devenv.exe", true) == 0 || String.Compare(fileName, "XDesProc.exe", true) == 0)
return;
SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, GetBrowserEmulationMode()); // Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
SetBrowserFeatureControlKey("FEATURE_AJAX_CONNECTIONEVENTS", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_DOMSTORAGE ", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_GPU_RENDERING ", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI ", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_DISABLE_LEGACY_COMPRESSION", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_LOCALMACHINE_LOCKDOWN", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_BLOCK_LMZ_OBJECT", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_BLOCK_LMZ_SCRIPT", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_DISABLE_NAVIGATION_SOUNDS", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_SCRIPTURL_MITIGATION", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_SPELLCHECKING", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_STATUS_BAR_THROTTLING", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_TABBED_BROWSING", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_VALIDATE_NAVIGATE_URL", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_WEBOC_DOCUMENT_ZOOM", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_WEBOC_POPUPMANAGEMENT", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_WEBOC_MOVESIZECHILD", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_ADDON_MANAGEMENT", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_WEBSOCKET", fileName, 1);
SetBrowserFeatureControlKey("FEATURE_WINDOW_RESTRICTIONS ", fileName, 0);
SetBrowserFeatureControlKey("FEATURE_XMLHTTP", fileName, 1);
}
private UInt32 GetBrowserEmulationMode()
{
int browserVersion = 7;
using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE'Microsoft'Internet Explorer",
RegistryKeyPermissionCheck.ReadSubTree,
System.Security.AccessControl.RegistryRights.QueryValues))
{
var version = ieKey.GetValue("svcVersion");
if (null == version)
{
version = ieKey.GetValue("Version");
if (null == version)
throw new ApplicationException("Microsoft Internet Explorer is required!");
}
int.TryParse(version.ToString().Split('.')[0], out browserVersion);
}
UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode. Default value for Internet Explorer 11.
switch (browserVersion)
{
case 7:
mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control.
break;
case 8:
mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8
break;
case 9:
mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.
break;
case 10:
mode = 10000; // Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 mode. Default value for Internet Explorer 10.
break;
default:
// use IE11 mode by default
break;
}
return mode;
}
你应该想出你自己的一组特性,并在WebBrowser初始化之前注册它们,例如,在主表单构造函数中:
public MainWindow()
{
SetBrowserFeatureControl();
InitializeComponent();
WebBrowser youtubePlayer = new WebBrowser();
this.Controls.Add(youtubePlayer);
youtubePlayer.Navigate("https://www.youtube.com/embed/dQw4w9WgXcQ");
//...
}
这个答案取自这里:c# web浏览器Ajax调用我发现它,而试图修复的javascript错误,我得到运行此代码时。
请访问YouTube嵌入式播放器和播放器参数
你在这里尝试的是IFrame嵌入。试试这个:打开记事本粘贴:
<iframe id="ytplayer" type="text/html" width="640" height="390"
src="http://www.youtube.com/embed/HLj0aLPLsys?autoplay=1"
frameborder="0"/>
保存为html文件并在浏览器上打开它。如果一切顺利,你会看到猫王唱的《午夜时分》。这类似于您试图让Web浏览器导航到的字符串。
现在返回到Visual Studio上的代码并替换之前的
youtubePlayer.Navigate("...");
输入以下代码:
youtubePlayer.Navigate("http://www.youtube.com/v/HLj0aLPLsys?version=3");//Embedded AS3 Player
或
youtubePlayer.Navigate("http://www.youtube.com/apiplayer?video_id=HLj0aLPLsys&version=3");//Chromeless AS3 Player
注意:你的问题的两个解决方案都被弃用了,但工作,是最接近的解决方案,可以达到
要使播放器自动运行视频而不受用户干扰,请在字符串末尾添加
&autoplay=1
允许webBrowser占用表单中可用的全部空间。
youtubePlayer.Dock = DockStyle.Fill;
也是为了摆脱恼人的脚本错误消息:
youtubePlayer.ScriptErrorsSuppressed = true;