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...

"seekTo fail in chromeless if video not already playing" in the YouTube APIs Developer Forum reports this in July of 2008. Suspicion is that it happens during queueing and with lower-quality videos.

[ updated 28 Mar 2009 ]

The solution is to call seekTo() after the playing event (1) has been received. So, the sequence is:

  1. onYouTubePlayerReady(playerId)
  2. onPlayerStateChange(newState == 1)
  3. 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.

[ posted by sstrader on 26 March 2009 at 9:36:55 AM in Programming ]