0
votes

I'm using the YouTube iframe API to implement a video player.

I'd like to be able to subscribe to an event that fires periodically during playback. For example, every 10 seconds, I'd like to capture and log the current playback time of the video.

The iframe API exposes events like onPlayerReady and onStateChange, but they don't tell me anything about the video while it's actually playing.

I'm not finding any answers in Google's documentation. Any ideas?

Thanks!

1
You could just have a timer started every 10 seconds on playback start (within onStateChange) and stop it on pausejuvian
Then to get the time simply use player.getCurrentTime(), which returns number of seconds since video started playingjuvian

1 Answers

1
votes

I answered my own question--you can use setInterval inside the onStateChange iframe API event like this:

function onPlayerStateChange(
    if (event.data === YT.PlayerState.PLAYING) {
        refresh_interval_id = setInterval(function () {
            var duration = videoPlayer.getDuration();
            var current_time = videoPlayer.getCurrentTime();
        }, 3000);
     } else if (event.data === YT.PlayerState.PAUSED || event.data === YT.PlayerState.ENDED) {
         clearInterval(refresh_interval_id);
    }
 }