0

I've an issue with the onStateChange event of the YouTube iframe player API. This event is not firing.

I saw YouTube iframe player API - OnStateChange not firing and tried everything described in the answer but I'm still facing the same issue.

I tried different snippet :

const player = new YT.Player('youtube-player', {
    height: '0',
    width: '0',
    videoId: videoID,
    playerVars: {
      autoplay: 1,
      loop: 0,
      html5: 1
    },
    events: {
      onReady: function (e) {
        r.setPlaybackQuality('small')
        $('#ytPlayer').fadeIn()

        player.addEventListener('onStateChange', function(e) {
          console.log('State is:', e.data);
          console.log(e.data == YT.PlayerState.ENDED, e.data, YT.PlayerState.ENDED)
          if (e.data == YT.PlayerState.ENDED)
            $('#ytPlayer').remove()
        });
      }
    }
  })

  setInterval( function() {
    var state = player.getPlayerState();
    console.log('State is:', state);

  }, 10);
const player = new YT.Player('youtube-player', {
    height: '0',
    width: '0',
    videoId: videoID,
    playerVars: {
      autoplay: 1,
      loop: 0,
      html5: 1
    },
    events: {
      onReady: function (e) {
        r.setPlaybackQuality('small')
        $('#ytPlayer').fadeIn()
      }
    }
  })

  player.addEventListener('onStateChange', function(e) {
    console.log('State is:', e.data);
    console.log(e.data == YT.PlayerState.ENDED, e.data, YT.PlayerState.ENDED)
    if (e.data == YT.PlayerState.ENDED)
      $('#ytPlayer').remove()
  });
const player = new YT.Player('youtube-player', {
    height: '0',
    width: '0',
    videoId: videoID,
    playerVars: {
      autoplay: 1,
      loop: 0
    },
    events: {
      onReady: function (e) {
        r.setPlaybackQuality('small')
        $('#ytPlayer').fadeIn()
      },
      onStateChange: function (e) {
        console.log(e.data == YT.PlayerState.ENDED, e.data, YT.PlayerState.ENDED)
        if (e.data == YT.PlayerState.ENDED)
          $('#ytPlayer').remove()
        // e.data === YT.PlayerState.ENDED && o(!1)
      }
    }
  })

What's going wrong ?

Thanks

0 Answers0