Youtube Player API - onYouTubePlayerReady()未执行

时间:2013-12-19 20:24:45

标签: youtube-api youtube-javascript-api

我正在使用youtube播放器javascript API自定义视频播放器:

<html>
<head>
</head>
<body>
  <iframe src="http://www.youtube.com/v/M7lc1UVf-VE?version=3&enablejsapi=1" frameborder="0"></iframe>

  <script type="text/javascript" charset="utf-8">
    function onYouTubePlayerReady(){
      console.log('done')
    }
  </script>
</body>
</html>

done未打印在控制台中,我缺少什么?

1 个答案:

答案 0 :(得分:0)

你必须包含iframe javascript库;一旦注入,它将自动调用名为onYouTubeIframeAPIReady的函数...在该函数中,您将创建一个绑定到iframe的玩家对象,并且它可以有事件监听器(例如在播放器准备好时通知您)。像这样的东西:

<html>
<head>
</head>
<body>
  <iframe id="player" src="http://www.youtube.com/v/M7lc1UVf-VE?version=3&enablejsapi=1" frameborder="0"></iframe>

  <script type="text/javascript" charset="utf-8">
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          events: {
            'onReady': onYouTubePlayerReady
          }
        });
      }

      function onYouTubePlayerReady(){
         console.log('done');
      }
  </script>
</body>
</html>