javascript Chrome 视频元素 canplay 事件未触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21103672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome video element canplay event not firing
提问by edoloughlin
In Chrome 31 on Windows 7 and Linux (Ubuntu 13.10) an event handler on a videoelement, registered for canplay(and oncanplay, just to make sure) never fires. When I inspect the DOM node, there's no oncanplayproperty. The specsays it should exist. Does anyone have any idea when, or if, Chrome might support this event?
在 Windows 7 和 Linux (Ubuntu 13.10) 上的 Chrome 31 中,视频元素上的事件处理程序,注册为canplay(和oncanplay,只是为了确保)永远不会触发。当我检查 DOM 节点时,没有oncanplay属性。该规范说,它应该存在。有谁知道 Chrome 何时或是否会支持此活动?
回答by brianchirls
Chrome doessupport 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.
Chrome确实支持该canplay
事件。您没有看到它,因为检查器只显示所有元素上的属性,而不仅仅是媒体元素。它也不显示loadedmetadata
、durationchange
等,但 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. So if the event fires before you attach the listener, it's too late.
我还没有看到您的代码,但我猜想您会看到事件触发的一个可能原因(假设您正在正确侦听它)是您错过了该事件。除非你经常跳过视频,canplay
否则只会触发一次。因此,如果事件在您附加侦听器之前触发,则为时已晚。
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
稍后可能会再次触发。