简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM