Reputation: 4964
When seeking to a time through the YouTube JavaScript API, it takes some time to the player.getCurrentTime()
to return the correct time. It, instead, for some time, returns the old time.
Example:
-> current video time: 10s
-> player.getCurrentTime()
-> returns 10s (correct)
-> player.seekTo(2s)
-> player.getCurrentTime()
-> returns 10s instead of 2s (incorrect)
-> wait 2 seconds...
-> player.getCurrentTime()
-> returns 12s (correct)
How can it be fixed?
Upvotes: 2
Views: 371
Reputation: 26
I'm also experiencing the same problem. I'm not sure how it can be fixed, exactly, but I am working around it by using something like
intervalId = setInterval( () => {
var ytTime = ytplayer.getCurrentTime()
[... do stuff here...]
}, 500)
... which isn't optimal, though it works for what I need it to do.
I've put an issue up on the google issue tracker that tracks 'Youtube API Service' issues, here: https://issuetracker.google.com/issues/142517061
but if I had to guess, this will be marked as a Works-As-Designed. The call to seekTo needs to do some work to perform the seek, and that work is probably async, so when we call getCurrentTime before that work is done, we're going to get a time that seems invalid, but actually the task simply isn't complete yet.
Note that after you do your call to seekTo, if you did something like
setTimeout( () => { console.log(ytplayer.getCurrentTime()) }, 2000 )
then you'll probably be seeing the correct time.
Upvotes: 1