Youtube - onStateChange处理一个视频而不是另一个视频

时间:2015-07-29 13:32:42

标签: javascript youtube youtube-api

当您更改YouTube视频ID时,相同的代码行为会有所不同。

问题在于,当您拉动进度滑块时,它会在一个视频上触发onStateChange事件,而不会触发另一个视频。检查下面的两个示例,并尝试在两个示例中拉出时间滑块并观察console.log。

为什么拉动时间滑块时只有一个视频会对 onStateChange 作出反应?

两个视频对播放/暂停按钮的反应都很好,但不是时间滑块...只有一个正在工作。

我错过了什么吗?任何帮助真的很感激

Youtube代码1(正常工作):http://jsfiddle.net/2t9omgwm/

home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/db.js:299                                                                                                 
throw err;                                                                                                                                              
     ^                                                                                                                                                 
TypeError: Converting circular structure to JSON                                                                                                                  
at Object.stringify (native)                                                                                                                                  
at ServerResponse.res.json (/home/ubuntu/workspace/node_modules/express/lib/response.js:185:19)                                                               
at ServerResponse.res.send (/home/ubuntu/workspace/node_modules/express/lib/response.js:117:21)                                                               
at /home/ubuntu/workspace/server.js:67:21                                                                                                                     
at MongoClient.open (/home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/mongo_client.js:103:5)                                                           
at Db.open (/home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/db.js:296:11)                                                                             
at process._tickCallback (node.js:442:13)

另一个视频无效:http://jsfiddle.net/ctgomt7t/

<!-- 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: '390',
      width: '640',
      videoId: 'M7lc1UVf-VE',
      iv_load_policy: 3,
      showinfo:0,
      //videoId: 'Fj73JF_bhjc',
      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) {
      console.log('state changed');
      //player.seekTo(0, true);
    if (event.data == YT.PlayerState.PLAYING && !done) {
      done = true;
    }
  }

</script>

`

1 个答案:

答案 0 :(得分:1)

由于以下原因之一,onPlayerStateChange事件被触发:

  

-1 =未启动

     

0 =已结束

     

1 =正在播放

     

2 =暂停

     

3 =缓冲

     

5 =视频提示

我认为你在更长的视频中看到的是当你移动滑块时(3)缓冲的事件。但是,在较短的视频上没有缓冲,因此您看不到该事件。

您可以通过将代码更改为console.log('state changed : ' + event.data)来查看此内容。