26 March 2009
YouTube API having issues with seekTo()
At around midnight last night, while working on code that uses embeded YouTube video with the YouTube API, my JavaScript that loads a video and seeks to a specific location started failing on the load (on one page) and failing on the seek (on another). I was working on an unrelated area and so took the opportunity to add more JavaScript logging via Log4JS (highly recommended). The problem persisted, code was moved around, and finally all logging was removed with the idea that watching the JavaScript somehow changed the event sequence. No luck. Eventually, I backed up the changed code and re-got the "working" versions from svn. Still broken. So I went to my reference app and received the sad news that the call is, in fact, no longer working as it had. Sometimes, it is that third party library that's causing problems!
The only reference I've found is a thread back in Feb 19th that actually duplicates my issues on the YouTube reference page: using the "Load video" with a non-zero "Start at" value fails to seek. The thread also contains a suggested workaround: wait until after the video is done buffering before performing the seekTo(). To be continued...
[ updated 28 Mar 2009 ]
The solution is to call seekTo() after the playing event (1) has been received. So, the sequence is:
- onYouTubePlayerReady(playerId)
- onPlayerStateChange(newState == 1)
- player.seekTo(offset, true)
In my tests, I'd also seen error 100 (requested video not found) returned if I called seekTo() too quickly after onYouTubePlayerReady() fires. So, this is probably not a YouTube issue, more of a Programmer Is a Bonehead (PIB) issue.
- Techniques after using Swift for a month posted by sstrader on 26 August 2015 at 11:41:51 PM
- Some thoughts on Ruby after finishing a week (minus one day) of training posted by sstrader on 15 December 2011 at 8:59:30 PM
- Links on the singleton pattern posted by sstrader on 9 December 2011 at 9:19:50 AM
- Phonebot posted by sstrader on 29 October 2011 at 6:37:05 PM
- The labeled break in Java posted by sstrader on 4 September 2011 at 11:27:56 AM