首页 文章

Youtube嵌入了iframe onReady和onStateChange事件未在Internet Explorer中触发

提问于
浏览
8

我几乎逐字复制了Youtube嵌入式播放器演示代码Youtube embedded player demo code - 更改是插入console.log以便我可以看到发生了什么 .

<!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) {
        console.log('onPlayerReady');
        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) {
          console.log('onPlayerStateChange');
          if (event.data == YT.PlayerState.PLAYING && !done) {
          setTimeout(stopVideo, 6000);
          done = true;
        }
      }
      function stopVideo() {
        player.stopVideo();
      }
    </script>
  </body>
</html>

这在Safari和Firefox中按预期工作,即一旦API加载选择的视频播放六秒钟,并且console.log包含事件正在触发的证据 . 但是在Internet Explorer中,事件onReady和onStateChange不会触发 . 如果我手动点击播放器中的播放控件但是我没有获得预期的onStateChange事件,则视频将播放 .

我在我的机器上检查了Flash的版本,它是:11.2.202.228,当前可用的是12.0.0.70 . 如果我在IE中禁用Flash(管理附加组件),则播放器按预期工作:播放6秒钟并触发事件 .

我没有更新Flash,因为虽然这可能会解决我的机器上的问题,但我的受众将仅限于那些可以做同样的事情,如果我更新了,我将无法查明问题是否通过其他方式解决 .

我也尝试过自己手动创建iframe,如上面的Youtube文档所述,这在Firefox和Safari中可以正常工作,但在IE中却没有 .

有一个Youtube demo player这在Firefox中完美运行,通过API工作进行播放,暂停和停止控制,当事件和函数调用写入"Statistics"部分的屏幕时,事件显然会触发 . 相比之下,当我在IE中看到这个时,通过API的播放,暂停和停止控制什么都不做 . 统计部分没有输出 . 播放和暂停的唯一方法是使用播放器上的按钮 . 如果我禁用Flash,则播放器和API可以正常工作 .

如果我尝试使用iframe非API嵌入,那么播放器可以正常工作,这让我相信这是使用API引起的问题 .

我知道有些人会说所有用户都应该拥有最新的Flash,说起来容易做起来难 . 许多学校,公司和图书馆都锁定了IE中的选项部分,因此用户可以为我的网站关闭Flash .

我想知道在使用API时是否有某种方法可以关闭Flash?

我搜索了Stackoverflow和其他地方,虽然我发现了类似的问题,但我没有找到任何修复 . I did find a somewhat similar promising lead但唉,我没有快乐 . Also hereherehereherehere .

我必须指出,当在本地机器和网站上使用时,即使我设置了origin参数,该问题也存在 .

我很感激建议和帮助 . 提前致谢 .

1 回答

  • 0

    我想知道在使用API时是否有某种方法可以关闭Flash?

    您可以使用以下选项强制YouTube播放器使用其html5播放器:

    playerVars: {html5: 1}
    

    将代码更改为:

    // 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',
        playerVars: {
          html5: 1
        },
        events: {
          'onReady': onPlayerReady,
          'onStateChange': onPlayerStateChange
        }
      });
    }
    

相关问题