Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome video element canplay event not firing

In Chrome 31 on Windows 7 and Linux (Ubuntu 13.10) an event handler on a video element, registered for canplay (and oncanplay, just to make sure) never fires. When I inspect the DOM node, there's no oncanplay property. The spec says it should exist. Does anyone have any idea when, or if, Chrome might support this event?

like image 769
edoloughlin Avatar asked Jan 13 '14 23:01

edoloughlin


1 Answers

Chrome does support the canplay event. You're not seeing it because the inspector only shows those properties that are on all elements, not just media elements. It also does not show loadedmetadata, durationchange, etc. but Chrome definitely supports those.

I haven't seen your code, but I would guess that a likely reason that you would see the event fire (assuming you're listening for it correctly) is that you've missed the event. Unless you're skipping around the video quite a bit, canplay will only fire one time. So if the event fires before you attach the listener, it's too late.

Instead, you can check the state, like so...

//assume you've already set up the video variable to point to your video element
if (video.readyState >= video.HAVE_FUTURE_DATA) {
    console.log('video can play!');
} else {
    video.addEventListener('canplay', function () {
        console.log('video can play!');
    }, false);
}

(Depending on what you're trying to accomplish, you may want to attach the event listener either way. The video's readyState can revert back if you run out of buffered data, and canplay might fire again later.

like image 96
brianchirls Avatar answered Oct 18 '22 20:10

brianchirls