Youtube - onStateChange工作在一个视频,但不是另一个

Youtube - onStateChange working on one video but not another

本文关键字:视频 一个 另一个 onStateChange 工作 Youtube      更新时间:2023-09-26

相同的代码行为不同,当你改变YouTube视频ID。

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

为什么当你拉时间滑块时,只有一个视频对onStateChange做出反应?

两个视频反应良好的播放/暂停按钮,但不是时间滑块…只有一个在工作

我错过了什么吗?非常感谢任何帮助

Youtube代码1 (working):(http://jsfiddle.net/2t9omgwm/)

<!-- 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>

在另一个视频中它不起作用:(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: '0Bmhjf0rKe8',
      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>

"

触发onPlayerStateChange事件的原因如下:

-1 = unstarted

0 = end

1 = play

2 = pause

3 =

5 =

我认为你在较长的视频中看到的是当你移动滑块时触发(3)缓冲的事件。然而,在较短的视频中,没有缓冲,所以你看不到事件。

您可以通过修改代码来读取console.log('state changed : ' + event.data)来看到这一点。