Ok, I found a way without giving up YouTubeโs built-in controls. The idea is to track the current time in a timeout callback and trigger an event (directly the callback function) whenever the time difference is โabnormalโ. Not the finest-grained method, because the check is performed twice a second, but it does the job when we need to handle seekTo events at a high level. Hope this helps others until Google decides to invest in an API update.
The following is a modified version of the example in the official YouTube iframe API documentation:
<!DOCTYPE html> <html> <body> <div id="player"></div> <script> var tag = document.createElement('script'); tag.src = "https://www.youtube.com/iframe_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubeIframeAPIReady() { console.log("YouTube API ready"); player = new YT.Player('player', { height: '390', width: '640', videoId: 'M7lc1UVf-VE', events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } function onPlayerReady(event) { console.log("YouTube video ready"); } function onPlayerStateChange(event) { switch(event.data) { case YT.PlayerState.CUED: console.log("YouTube video CUED"); break; case YT.PlayerState.PLAYING: console.log("YouTube video PLAYING"); isPlaying = true; checkSeek(); break; case YT.PlayerState.BUFFERING: console.log("YouTube video BUFFERING"); isPlaying = false; break; case YT.PlayerState.PAUSED: console.log("YouTube video PAUSED"); isPlaying = false; break; case YT.PlayerState.ENDED: console.log("YouTube video ENDED"); isPlaying = false; break; } } </script> </body> </html>
source share