html5-视频下载进度仅适用于 Firefox
我有以下函数来计算视频的加载进度(很常见):
function updateProgressBar (video) {
if (video.buffered.length > 0) {
var percent = (video.buffered.end(0) / video.duration) * 100;
$('#loading').css({'width': percent + '%'});
console.log(percent);
if (percent == 100) {
console.log('video loaded!');
//everything is loaded, do something.
$(video).unbind('loadeddata canplaythrough playing'); //prevents the repetition of the callback
}
}
}
...绑定到 $(document).ready 函数内的“进度”事件(以及其他一些作为安全措施):
var videoTest = document.getElementById("videoTest");
$('#videoTest').bind('progress', function () {
updateProgressBar (videoTest);
});
$('#videoTest').bind('loadeddata', function () {
updateProgressBar (videoTest);
});
$('#videoTest').bind('canplaythrough', function () {
updateProgressBar (videoTest);
});
$('#videoTest').bind('playing', function () {
updateProgressBar (videoTest);
});
您可以在此处查看实例: http://www.hidden-workshop.com/ video/
如您所见,一切正常在 Firefox 上效果很好,但在其他浏览器中,“percent”变量永远不会达到预期的“100”值;该函数总是停止在 90~,因此我无法知道视频何时完成加载(这对于我想要做的事情至关重要)。
这就像“进度”事件在我获得“百分比”的最终值之前停止工作,因为如果您单击“播放”按钮,“播放”事件将触发,然后成功计算并读取“百分比”变量的最后一个值(即 100)。
我是否遗漏了什么,或者这是一个常见问题?我可以使用任何解决方法吗?
提前致谢!
I have the following function to calculate the loading progress of a video (quite common):
function updateProgressBar (video) {
if (video.buffered.length > 0) {
var percent = (video.buffered.end(0) / video.duration) * 100;
$('#loading').css({'width': percent + '%'});
console.log(percent);
if (percent == 100) {
console.log('video loaded!');
//everything is loaded, do something.
$(video).unbind('loadeddata canplaythrough playing'); //prevents the repetition of the callback
}
}
}
...bound to the 'progress' event (and some others as a safety meassure) inside a $(document).ready function:
var videoTest = document.getElementById("videoTest");
$('#videoTest').bind('progress', function () {
updateProgressBar (videoTest);
});
$('#videoTest').bind('loadeddata', function () {
updateProgressBar (videoTest);
});
$('#videoTest').bind('canplaythrough', function () {
updateProgressBar (videoTest);
});
$('#videoTest').bind('playing', function () {
updateProgressBar (videoTest);
});
You can view a live example here: http://www.hidden-workshop.com/video/
As you can see, it all works well on firefox, but in the rest of the browsers, the 'percent' variable never reaches the value of '100' as it would be expected; the function always stops at 90~, and thus I'm unable to know when the video has finished loading (vital for what I'm trying to do).
It's like the 'progress' event stops working before I can get the final value of 'percent', because if you click the 'play' button, the 'playing' event fires and then successfully calculates and reads the 'percent' variable's last value (which is 100).
Am I missing something, or is it a common issue? Is there any workaround I could use?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另外,视频的“预加载”属性值为“auto”并不能保证用户代理将加载整个视频。
Also value "auto" for "preload" attribute of video not guarantee that user agent will load whole video.