Youtube 嵌入 Iframe - 事件未在本地触发

问题描述 投票:0回答:3

我刚刚注意到,当我在本地进行测试时,Youtube 事件(onReady、onStateChange)不再触发,但当我将代码上传到 JsFiddle 时,Youtube 事件(onReady、onStateChange)不再触发。我决定从 Youtube Player API 复制并粘贴代码,我可以验证它确实无法正常工作。还有其他人有这个问题吗?此问题出现在 Chrome、Firefox、IE、Safari 和 Opera 上。

编辑 - 当我在本地

console.log(player);
时,我发现它缺少很多 Youtube 功能,例如 JsFiddle 中存在的
seekTo()
setVolume()
showVideoInfo()
。我不确定为什么会发生这种情况,但我相信这可能是我的问题的根源。有什么想法吗?

http://jsfiddle.net/8ZmKx/

<!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: '190',
          width: '140',
          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('ready');
        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('change');
        if (event.data == YT.PlayerState.PLAYING && !done) {
          setTimeout(stopVideo, 6000);
          done = true;
        }
      }
      function stopVideo() {
        player.stopVideo();
      }
    </script>
  </body>
</html>
javascript youtube youtube-api
3个回答
5
投票

Youtube iframe API 目前似乎已损坏,但我还没有看到官方确认。

另请参阅:

在模板中显式写入 iframe 时,onReady 回调不会触发

YouTube API onPlayerReady 未触发

编辑:这是修复程序:https://code.google.com/p/gdata-issues/issues/detail?id=5670#c6

这是上面链接中答案的直接引用:

快速修复方法是添加 origin=http://www.example.com (替换 您的服务器拥有完整的域名;确保使用 http:// 或 https:// 根据您的网站情况)添加到播放器的 src= 属性 iframe 元素。请确保 origin= 属性的值 与主机域和 URL 方案完全匹配。例如

<iframe
  type="text/html"
  width="640"
  height="480"
  src="https://www.youtube.com/embed/VIDEO_ID?enablejsapi=1&origin=https://www.example.com"
  frameborder="0">
</iframe>

我目前正在与工程团队合作,以确定是否 origin= 是否需要继续前进或者这个新的 要求可以恢复,但这是立即修复。道歉 同时由于破损和缺乏先进的沟通。

如果使用 YT.Player() 构造函数创建 iframe 元素 对你来说这不是问题——这只是一个极端情况 明确将 iframe 元素作为其一部分的开发人员 他们页面的 HTML。

我实施了上面的修复,它对我有用。


1
投票

我已经进行了修复,实现了“origin=”参数,修复了原始错误并触发状态更改事件。

现在原始参数导致了错误。删除了参数,它又可以工作了。


0
投票

将完整的 URL(例如“https://www.example.net”)添加到 URL 的 origin 参数对我有用

© www.soinside.com 2019 - 2024. All rights reserved.