简体   繁体   English

Chrome视频元素可播放未触发的事件

[英]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. 在Windows 7和Linux(Ubuntu 13.10)上的Chrome 31中, 视频元素上的事件处理程序,注册canplay (和oncanplay ,只是为了确保)永远不会触发。 When I inspect the DOM node, there's no oncanplay property. 当我检查DOM节点时,没有oncanplay属性。 The spec says it should exist. 规范说它应该存在。 Does anyone have any idea when, or if, Chrome might support this event? 有没有人知道Chrome何时或是否支持此活动?

Chrome does support the canplay event. Chrome 确实支持canplay活动。 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. 它也没有显示loadedmetadatadurationchange等,但Chrome肯定支持那些。

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. 除非您在视频中略微跳过, canplay只会触发一次。 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. (根据您要完成的任务,您可能希望以任一方式附加事件侦听器。如果您的缓冲数据用完,视频的readyState可以恢复,并且canplay可能会再次触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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