Youtube API不适用于<iframe> </iframe>

时间:2014-07-22 03:34:15

标签: api iframe youtube

在没有触发onPlayerReady和onPlayerStateChage之后,我是否对代码做错了?

我尝试不使用<div id="player></div>

任何人和我有同样的问题?

如果您对我的问题有任何疑问,请与我们联系。

谢谢!

<!DOCTYPE html>
<html>
  <body>
    <!-- 1. The <iframe> (and video player) will replace this <div> tag.-->
    <iframe id="player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com" frameborder="0"></iframe>

    <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('player1', {
          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 === 0) {            
            alert('done');
        }

        /*if (event.data == YT.PlayerState.PLAYING && !done) {
          setTimeout(stopVideo, 6000);
          done = true;
        }*/
      }
      function stopVideo() {
        player.stopVideo();
      }
    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

代码加载视频播放器就好了。问题是你没有得到自动播放? 这个beahviour的一个可能原因是:

no autoplay in iframe HTML5 player on mobile (Android Chrome and Firefox)?

此外,this link建议您启用并更新闪存。