The reason your code isn’t working is because YouTube’s getPlayerState()
and Vimeo’s getPaused()
don’t return results instantly — you need to use their APIs properly.
For YouTube, create the player using new YT.Player
after the API is ready, then call getPlayerState()
.
For Vimeo, getPaused()
returns a Promise, so you must use .then()
to get the value.
YouTube Example:
<script src="https://www.youtube.com/iframe_api"></script>
<iframe id="yt-player" width="560" height="315"
src="https://www.youtube.com/embed/n1tswFfg-Ig?enablejsapi=1"
frameborder="0" allowfullscreen>
</iframe>
<script>
let ytPlayer;
function onYouTubeIframeAPIReady() {
ytPlayer = new YT.Player('yt-player', {
events: {
onReady: checkYTState
}
});
}
function checkYTState() {
// 1 = playing, 2 = paused
const state = ytPlayer.getPlayerState();
console.log('YouTube state:', state);
}
</script>
Vimeo Example:
<script src="https://player.vimeo.com/api/player.js"></script>
<iframe id="vimeo-player" src="https://player.vimeo.com/video/237596019"
width="640" height="360" frameborder="0" allowfullscreen>
</iframe>
<script>
const vimeoPlayer = new Vimeo.Player('vimeo-player');
function checkVimeoState() {
vimeoPlayer.getPaused().then(function(paused) {
console.log('Vimeo paused:', paused);
});
}
checkVimeoState();
</script>