paulirish / lite-youtube-embed

A faster youtube embed.
https://paulirish.github.io/lite-youtube-embed/
Other
5.92k stars 274 forks source link

I am using lite youtube embed and I want to get onStateChange player.getPlayerState(): #174

Closed FrostBite70 closed 1 month ago

FrostBite70 commented 8 months ago

I am new in studying and learning javascript, can you add a bit of changes to the code for the onstatechange, I checked and tried to modify the code, trying to add it but I fail. The code I tried to make with getPlayerState() only triggers the chosen video id to play but not to get the values stated in: https://developers.google.com/youtube/iframe_api_reference#getPlayerState

async function getPlayerState(number, id) {
  let num = document.querySelector(`lite-youtube[id='${id}']`);
  let player = await num.getYTPlayer();
  player?.getPlayerState(number, true);
}

playlist.addEventListener('click', () => {
    this.getPlayerState(1, '1')       // the string inside works and plays the specific video id but it does not get the PlayerState value 
    if (this.getPlayerState(0, '1')) {  
    this.getPlayerState(1, '2')      //<=this always trigger the specific video id but it plays no matter what is changed on the ifcondition
    }
});
paulirish commented 1 month ago

not sure what you're trying to do.. but.. getPlayerState() doesn't take any arguments. just call it like player.getPlayerState().. and it'll return one of the numbers in the docs.

there are separate methods like .playVideo() and .pauseVideo() etc if you want to do that ..

but that's clearly up to you.

good luck