Winforms 无法在Windows.Forms.WebBrowser控件中播放YouTube视频

Winforms 无法在Windows.Forms.WebBrowser控件中播放YouTube视频,winforms,youtube-api,webbrowser-control,youtube-iframe-api,Winforms,Youtube Api,Webbrowser Control,Youtube Iframe Api,我试图简单地将以下内容分配给WebBrowser控件的DocumentText属性 <!DOCTYPE html> <html> <body> <!-- 1. The <iframe> (and video player) will replace this <div> tag. --> <div id="player"></div> <script>

我试图简单地将以下内容分配给
WebBrowser
控件的
DocumentText
属性

<!DOCTYPE html>
<html>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player"></div>

    <script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');

      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: 'M7lc1UVf-VE',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }

      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        event.target.playVideo();
      }

      // 5. The API calls this function when the player's state changes.
      //    The function indicates that when playing a video (state=1),
      //    the player should play for six seconds and then stop.
      var done = false;
      function onPlayerStateChange(event) {
        if (event.data == YT.PlayerState.PLAYING && !done) {
          setTimeout(stopVideo, 6000);
          done = true;
        }
      }
      function stopVideo() {
        player.stopVideo();
      }
    </script>
  </body>
</html>

// 2. 此代码异步加载IFrame播放器API代码。
var tag=document.createElement('script');
tag.src=”https://www.youtube.com/iframe_api";
var firstScriptTag=document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(标记,firstScriptTag);
// 3. 此函数用于创建(和YouTube播放器)
//API代码下载后。
var播放器;
函数onyoutubeiframeapiredy(){
player=新的YT.player('player'{
高度:“390”,
宽度:“640”,
videoId:'M7lc1UVf VE',
活动:{
“onReady”:onPlayerReady,
“onStateChange”:onPlayerStateChange
}
});
}
// 4. 当视频播放器准备就绪时,API将调用此函数。
函数onPlayerReady(事件){
event.target.playVideo();
}
// 5. 当播放器的状态改变时,API调用此函数。
//该功能指示播放视频时(状态=1),
//玩家应该玩六秒钟,然后停下来。
var done=false;
函数onPlayerStateChange(事件){
如果(event.data==YT.PlayerState.PLAYING&&!done){
设置超时(stopVideo,6000);
完成=正确;
}
}
函数stopVideo(){
player.stopVideo();
}
这在运行时给了我以下脚本错误:

事实证明,即使是以下简化代码也会产生相同的错误:

webBrowser1.DocumentText = "<!DOCTYPE html><html><body><script src='https://www.youtube.com/iframe_api'></script></body></html>";
webBrowser1.DocumentText=“”;

因此,
WebBrowser
控件似乎无法加载iframe API。这里有什么问题?或者如何进一步调查此错误?

我找到了WebBrowser的解决方案。首先,我们必须强迫WebBrowser使用我们机器上的最新IE运行。 第二,我必须通过ASP.NETWebAPI和类似Katana的方式来流式传输HTML,使用WebBrowser导航到

设置DocumentText不起作用,因为WebBrowser在本地加载html时存在许多安全限制


另一个简单的解决方案是使用其他浏览器引擎,如Gecko或CefSharp。

您能解决您的问题吗?我的问题与您的相同。不,我无法使它与
WebBrowser
控件一起工作。我现在使用的是Gecko引擎(Geckofx45 Nuget软件包)。只有这个答案的第一步,然后是webBrowser。导航到youtube的嵌入式链接对我来说很好。