HTML5 视频加载时间

发布于 2024-10-27 04:39:13 字数 1592 浏览 1 评论 0原文

我正在尝试计算 html 5 视频的测量时间。我使用 Javascript 来监听 html5 视频事件 loadstartcanplaythrough 使用:

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);
    
} 

但是,timestartLoadtimeFinishLoad 均未定义。我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆那么伤 2024-11-03 04:39:13

您的代码存在一些(复制和粘贴)语法问题。

var timeInit = Date.now(), timeLoad, timeCanPlay;

$("movie").addEventListener('loadstart', function(){
  timeLoad = Date.now();
    $("t1").innerHTML = "load: " + (timeLoad - timeInit) + " msecs";
});

$("movie").addEventListener('canplaythrough', function(){
  timeCanPlay = Date.now();
  $("t2").innerHTML = "canplay: " + (timeCanPlay - timeLoad) + " msecs";
});

$("movie").src = "http://ia600208.us.archive.org/12/items/FarSpeak1935/FarSpeak1935_512kb.mp4";

$("movie").play();

尝试一下:http://jsfiddle.net/noiv/98xZP/

Your code has some (copy & paste) syntax problems.

var timeInit = Date.now(), timeLoad, timeCanPlay;

$("movie").addEventListener('loadstart', function(){
  timeLoad = Date.now();
    $("t1").innerHTML = "load: " + (timeLoad - timeInit) + " msecs";
});

$("movie").addEventListener('canplaythrough', function(){
  timeCanPlay = Date.now();
  $("t2").innerHTML = "canplay: " + (timeCanPlay - timeLoad) + " msecs";
});

$("movie").src = "http://ia600208.us.archive.org/12/items/FarSpeak1935/FarSpeak1935_512kb.mp4";

$("movie").play();

Try out: http://jsfiddle.net/noiv/98xZP/:

趁微风不噪 2024-11-03 04:39:13

嘿,我明白了。
原因似乎是由于浏览器的快速响应能力,它甚至在捕获事件之前就获取了数据。 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/

滥情稳全场 2024-11-03 04:39:13

虽然我无法发表评论,但我已经在 @noiv 上面发布的小提琴中添加了一个叉子,因为小提琴有一些 JS 错误。感谢@noiv 让我走上了正确的方向。

http://jsfiddle.net/truedat101/Gbfj2/7/

$("movie").addEventListener('loadstart', function(){
    timeLoad = Date.now();
    console.log("loadstart event time: " + timeLoad + ", delta: " + (timeLoad - timeInit));
    // alert("loadstart event time: " + timeLoad + ", delta: " + (timeLoad - timeInit));
});

值得注意的是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/

$("movie").addEventListener('loadstart', function(){
    timeLoad = Date.now();
    console.log("loadstart event time: " + timeLoad + ", delta: " + (timeLoad - timeInit));
    // alert("loadstart event time: " + timeLoad + ", delta: " + (timeLoad - timeInit));
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文