javascript - YouTube 播放器 API 未按文档说明工作

标签 javascript api youtube

YouTube Player API 目前似乎无法正常工作。播放器将正确初始化,但没有任何方法或事件有效。

<div class="flex">
    <div id="player"></div>
    <div id="log"></div>
</div>

<script>
    // for convenience
   var isPlayerReady = false;
   var logEl = document.getElementById('log');

   function addToLog(text) {
       logEl.innerHTML = logEl.innerHTML + text + '<br>';
   }

   window.setTimeout(function () {
       if (!isPlayerReady) {
           addToLog('5 seconds have passed and the player has not fired the onReady event. This is a good indication that the API is not working properly.');
       }
   }, 5000);
   
   // from here on, the code is a direct copy of an example from the official docs

   // 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() {
       addToLog('onYouTubeIframeAPIReady fired, loading Player');
       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) {
       isPlayerReady = true;
       addToLog('onPlayerReady fired');
       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) {
       addToLog('onPlayerStateChange fired');
       if (event.data == YT.PlayerState.PLAYING && !done) {
           setTimeout(stopVideo, 6000);
           done = true;
       }
   }

   function stopVideo() {
       player.stopVideo();
   }
    </script>

我创建了一个简单的 codepen 来监控问题 here .此示例的大部分内容是 official docs 中的示例之一的直接副本。 .

最佳答案

[编辑] 从此 Google Issue Tracker 找到解决方案.好像是浏览器的问题。
试试那些:

  • 清除缓存,包括 cookie
  • 尝试禁用 chrome://flags/#same-site-by-default-cookies把它放回去(为我工作)

  • 希望有帮助🙃
    ===
    你不是一个人!我有使用 on... 的代码几天前工作的事件(onReady 等)突然停止工作而没有更改代码。
  • 它已发布在 Google 问题跟踪器上(参见 here)
  • 这里是 another stackoverflow post陈述同样的问题
  • 它甚至渗透到像 react-youtube 这样的子库。 (见 Issue #260 )

  • 看起来它开始于 9 月 9 日,与 Youtube Data API 版本一致(参见 revision history for Sept. 9)。但我在他们的列表中看不到任何会损害 iframe 事件/功能的东西🤔
    这就是我所取得的成就,希望能给某人一个提示,以解决这个问题。 🤷🏼‍♂️ 将在发现时更新此评论。

    关于javascript - YouTube 播放器 API 未按文档说明工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63921369/

    相关文章:

    javascript - 为什么 jQuery 单击事件处理程序不能为动态加载的 DOM 元素正常工作?

    Android SDK 不在 linux (ubuntu) 上安装组件

    c# - 使用 Outlook API 获取特定文件夹

    javascript - 如何通过 JavaScript 知道用户何时开始或停止视频?

    angularjs - 无法从 ionic 中的 Controller 将 URL 传递给 View

    Javascript点击监听器循环两次

    javascript - 使用js创建允许将可拖动元素放入其中的框

    JavaScript 执行 Ruby 脚本

    用于更改音轨的 Windows API

    php - 如何组合功能以获取多种类型的信息