JW Player – sendEvent is not a function
Ok, I’ve spent a whole friggin day on this problem so hopefully this post will help save some people some time…
We’ve been working on a video site recently that uses the fantastic JW Player to play the .flv flash files. For some reason, there were some issues with using a tabbed interface we’d made in conjunction with the JW Player.
After changing from a tab with a text page back to the video page, the following error was coming up:
Error: $("#player1")[0].sendEvent is not a function
The code causing this error was a little routine to load a different video into the player when someone clicks on a different tab:
$('#player1')[0].sendEvent('LOAD',obj);
After a whole day of trial and error and putting alerts all over my JS code, I finally worked out what the hell was going on.
Even though my JS code was waiting until the document had been loaded (through jQuery’s $(document).ready(function() { blah..blah), the JW Player wasn’t loading in that time. It turns out even though our tabbed pages were powered by simple show/hide CSS, going from the video tab to a non-video tab and back made the JW Player have to load again.
The sendEvent is not a function error was simply because the JW Player hadn’t loaded yet, so the player1 object didn’t exist yet!
The solution? A simple delay:
setTimeout(function() { $('#player1')[0].sendEvent('LOAD',obj); }, 200);
Now this is not a pretty solution at all. Maybe 200 ms is enough time for the player to load on my computer, but what about on another set up? Newer versions of the JW Player automatically call a particular function when it finishes loading (playerReady()), but I wasn’t able to find a quick and easy way to check the player was loaded.
If anyone knows how to get playerReady() working nicely with jQuery’s $(document).ready() I’d love to hear about it. From the comments on the JW Player site, it seems the playerReady() function is a little flakey in any case. But is a setTimeout any better? Hmm…