HTML5 视频加载时间
我正在尝试计算 html 5 视频的测量时间。我使用 Javascript 来监听 html5 视频事件 loadstart
和 canplaythrough
使用:
media.addEventListener('loadstart'getStartTime(){
startTime = new Date().getTime();},
false)
和类似的 endTime
事件设置为 canplaythrough
> 倾听。 但是我无法获得任何数据。
有人可以指导我如何使用 Javascript 测量视频加载时间吗?
感谢您的回复,但解决方案是我相信使用 jQuery;但是,我想知道是否可以通过 Javascript 实现。我已附上代码的副本:
function loadVideo(){
var timeNow = Date.now(), timeStartLoad, timeFinishLoad;
myVideos = new Array();
myVideos[0] = "trailer.mp4";
myVideos[1] = "trailer.ogg";
myVideos[2] = "trailer.m4v";
var videoId = document.getElementById('idForVideo');
var video = document.createElement('video');
for(var i=0; i<myVideos.length; i++){
var source = document.createElement('source');
source.setAttribute('src', myVideos[i]);
video.appendChild(source);
}
video.load();
video.addEventListener('loadstart', function(){
timeStartLoad = Date.now() - timeNow;
}, false);
video.addEventListener('hasenoughdata', function(){
timeFinishLoad = Date.now() - timeStartLoad;
}, false);
idForVideo.appendChild(video);
newDiv = document.getElementById('newDiv');
newDiv.innerHTML = "BodyLoad: " + timeNow + " " + "; Video Load: " + timeStartLoad + "; Video Loaded: " + timeFinishLoad;
//alert(timeStartLoad);
}
但是,timestartLoad
和 timeFinishLoad
均未定义。我的 html 正文有链接到此函数的 onload
方法。
I am trying to calculate the measure time of html 5 video. I use Javascript to listen to html5 video event loadstart
and canplaythrough
using:
media.addEventListener('loadstart'getStartTime(){
startTime = new Date().getTime();},
false)
and similar for endTime
with event set as canplaythrough
to listen.
However I could not get any data.
Can someone please guide me how to measure video load time using Javascript.
Thank you for your response, but the solution is I believe using jQuery; however, I was wondering if it is possible from Javascript. I have attached a copy of my code:
function loadVideo(){
var timeNow = Date.now(), timeStartLoad, timeFinishLoad;
myVideos = new Array();
myVideos[0] = "trailer.mp4";
myVideos[1] = "trailer.ogg";
myVideos[2] = "trailer.m4v";
var videoId = document.getElementById('idForVideo');
var video = document.createElement('video');
for(var i=0; i<myVideos.length; i++){
var source = document.createElement('source');
source.setAttribute('src', myVideos[i]);
video.appendChild(source);
}
video.load();
video.addEventListener('loadstart', function(){
timeStartLoad = Date.now() - timeNow;
}, false);
video.addEventListener('hasenoughdata', function(){
timeFinishLoad = Date.now() - timeStartLoad;
}, false);
idForVideo.appendChild(video);
newDiv = document.getElementById('newDiv');
newDiv.innerHTML = "BodyLoad: " + timeNow + " " + "; Video Load: " + timeStartLoad + "; Video Loaded: " + timeFinishLoad;
//alert(timeStartLoad);
}
However I get undefined for both timestartLoad
and timeFinishLoad
. My html body has onload
method linked to this function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码存在一些(复制和粘贴)语法问题。
尝试一下:http://jsfiddle.net/noiv/98xZP/:
Your code has some (copy & paste) syntax problems.
Try out: http://jsfiddle.net/noiv/98xZP/:
嘿,我明白了。
原因似乎是由于浏览器的快速响应能力,它甚至在捕获事件之前就获取了数据。 Opera 提供的解决方案似乎有效。在内联脚本中包含事件侦听器,并为窗口对象创建 addEventListener。
更多详情请参见:
http://dev.opera.com/articles/查看/consistency-event-firing-with-html5-video/
Hey, I figured it out.
The reason seems to be due to the fast responsiveness of the browser that it fetches the data before even the event is catched. A solution as provided by opera seems to work. Include the event listener at the inline script and make addEventListener for window object.
More details at:
http://dev.opera.com/articles/view/consistent-event-firing-with-html5-video/
虽然我无法发表评论,但我已经在 @noiv 上面发布的小提琴中添加了一个叉子,因为小提琴有一些 JS 错误。感谢@noiv 让我走上了正确的方向。
http://jsfiddle.net/truedat101/Gbfj2/7/
值得注意的是whatwg正在尝试在浏览器中实现视频指标的统一,以便浏览器支持一些通用属性,尽管这项工作似乎已经跨党派界限进行划分,Mozilla 支持他们自己的指标,Chromium 团队支持他们的,Apple Safari 支持他们的。
While I can't comment, I have added a fork to the fiddle posted above by @noiv , as the fiddle had some JS errors. Thanks @noiv for putting me on the right direction.
http://jsfiddle.net/truedat101/Gbfj2/7/
It is worth noting the whatwg is attempting to get some uniformity around video metrics in the browser, so that there will be some common attributes supported by the browser, though it appears already that this work will be split across party lines, with Mozilla supporting their own metrics, Chromium team supporting theirs, and Apple Safari supporting theirs.