Youtube - onStateChange works on one video but not another

This same code behaves differently when you change the YouTube video ID.

The problem is that when the progress slider is clicked, it fires the onStateChange event on one video but not the other. Check out the two examples below and try to pull the timer on both examples and look at your console.log.

Why is only one of the videos responding to onStateChange when you drag the time slider?

Both videos respond fine to the play / pause button, but not the time slider ... only one works.

Am I missing something? Any help is really appreciated

Youtube code 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 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>

      

And it doesn't work with another video: ( 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 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>

      

`

+3


source to share


1 answer


The event is onPlayerStateChange

triggered for one of the following reasons:

-1 = non-standard

0 = ended

1 = play

2 = suspended

3 = buffering

5 = video



I think what you see in the longer video is firing on the buffering event (3) when the slider is moved. However, there is no buffering in the shorter video, so you don't see this event.

This can be seen by changing the code to console.log('state changed : ' + event.data)

.

+1


source







All Articles