Youtube视频播放器仅播放最后一个视频

时间:2014-07-26 19:10:45

标签: javascript video youtube

使用带有JS

的Google嵌入式播放器

我在.php page

上有两个玩家

第一个视频无法播放,第二个视频播放。

我很确定因为我用第二个函数覆盖了第一个函数。 我在哪里错了?

视频0

  <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player0"></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 player0;
      function onYouTubeIframeAPIReady() {
        player0 = new YT.Player('player0', {
          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) {
        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 == YT.PlayerState.PLAYING && !done) {
          setTimeout(stopVideo, 6000);
          done = true;
        }
      }
      function stopVideo() {
        player0.stopVideo();
      }
    </script>

视频1

<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
    <div id="player1"></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 player1;
      function onYouTubeIframeAPIReady() {
        player1 = new YT.Player('player1', {
          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) {
        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 == YT.PlayerState.PLAYING && !done) {
          setTimeout(stopVideo, 6000);
          done = true;
        }
      }
      function stopVideo() {
        player1.stopVideo();
      }
    </script>

1 个答案:

答案 0 :(得分:1)

你是对的,你正在覆盖你的功能。例如,onYouTubeIframeAPIReady()只会创建第二个玩家。你可以猜到,应该这样写:

function onYouTubeIframeAPIReady() {
    //declare your first player
    //declare your second player
}

Youtube API文档中的done变量仅适用于一位玩家。如果您想要多个变量,则应创建多个变量或数组。但为了清楚起见,我不会进入那个。

创建多个玩家并让他们玩游戏的示例:

<div id="player0"></div>
<div id="player1"></div>

<script>

    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 player0, player1;

    function onYouTubeIframeAPIReady() {
        player0 = new YT.Player('player0', {
            height: '390',
            width: '640',
            videoId: 'M7lc1UVf-VE',
            events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
            }
        });

        player1 = new YT.Player('player1', {
            height: '390',
            width: '640',
            videoId: 'M7lc1UVf-VE',
            events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
            }
        });
    }

    function onPlayerReady(event) {
        event.target.playVideo();
    }

    function onPlayerStateChange(event) {
        //do something with 'event.target' if you need to
    }

</script>

JS Fiddle Demo

需要更多示例或解释吗?请告诉我们。