views:

987

answers:

2

I'm working on a site for a client and they're insistent on using HTML5's video tag as the delivery method for some of their video content. I currently have it up and running with a little help from http://videojs.com/ to handle the Internet Explorer Flash fallback.

One thing they've asked me to do is, after the videos finish playing (they're all a different length), fade them out and then fade a picture in place of the video --- think of it like a poster frame after the video.

Is this even possible? Can you get the timecode of a currently playing movie via Javascript or some other method? I know Flowplayer (http://flowplayer.org/demos/scripting/grow.html) has an onFinish function, is that the route I should take in lieu of the HTML5 video method? Does the fact that IE users will be getting a Flash player require two separate solutions?

Any input would be greatly appreciated. I'm currently using jQuery on the site, so I'd like to keep the solution in that realm if at all possible. Thanks!

+1  A: 

There is an OnEnded event associated with the video tag. However, it does not work for me in the current version of Google Chrome.

http://stackoverflow.com/questions/2925851/html-5-video-onended-event-not-firing

and see also

http://stackoverflow.com/questions/2741493/how-do-you-detect-html5-video-events

For a general-purpose solution (supports video tag with fallback see)

http://camendesign.com/code/video_for_everybody or http://www.kaltura.org/project/HTML5_Video_Media_JavaScript_Library or http://www.mediafront.org/

Eric J.
That's definitely helpful, but would a separate solution be required for IE users who, in my case, are being delivered a Flash player instead of native video?
Andrew
@Andrew: Posed three solutions that use the video tag but also provide a fallback to use flash video.
Eric J.
+6  A: 

You can view a complete list of events in the spec here.

For example:

$("video").bind("ended", function() {
   alert("I'm done!");
});

You can bind to the event on the element like anything else in jQuery...as for your comment question, whatever element you're delivering for IE, yes, it would need a separate handler rigged up to whatever event it provides.

For the other question about timecode, the timeupdate event occurs when it's playing, and the durationchange event occurs when the overall duration changes. You can bind to and use them just like I showed with the ended event above. With timeupdate you'll probably want the currentTime property, with durationchange you'll want the duration property, each of which you get directly off the DOM object, like this:

$("video").bind("durationchange", function() {
   alert("Current duration is: " + this.duration);
});
Nick Craver
@Nick: Did you get this code to work? What browser?
Eric J.
@Eric - I have an old test page here, works in chrome I'm on 6, but this worked back in 5 when I made it), not sure which others...they're just normal events though, if the browser supports them this should work.
Nick Craver
@Nick: Could you have a quick look at my old (unanswered) post http://stackoverflow.com/questions/2925851/html-5-video-onended-event-not-firing and let me know if you see the problem?
Eric J.
@Eric - I can't say for sure without the media files if anything else is wrong, but remove the `on` from `onended`, it's just `ended` when you're calling `.bind()`, same with the `onmouseover` event, it's just `mouseover` for example.
Nick Craver
Nick, thanks for the quick answer. There are only a few videos throughout the site and all of them will be behaving similarly, but a quick test of this showed success in Firefox, Safari, and Chrome. Since Flowplayer is serving up IE, I'll use the built-in onFinish function for consistency. You rock!
Andrew