html5-视频下载进度仅适用于 Firefox

发布于 2024-12-22 18:01:14 字数 1437 浏览 0 评论 0原文

我有以下函数来计算视频的加载进度(很常见):

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 技术交流群。

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

发布评论

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

评论(1

扛起拖把扫天下 2024-12-29 18:01:14
var videoElement = null; //TODO set reference to video element

var checkVideoLoadingProgressTimerDelay = 50;
var checkVideoLoadingProgressTimerID = -1;

function getVideoLoadingProgress(){
    var end = 0;
    if(videoElement.buffered.length >= 1){
        end = videoElement.buffered.end(0);
    }
    var progress = end / videoElement.duration;
    progress = isNaN(progress) ? 0 : progress;
    return progress;
};

function startCheckVideoLoadingProgressTimer(){
    checkVideoLoadingProgressTimerID = 
        setTimeout(checkVideoLoadingProgressTimerHandler, checkVideoLoadingProgressTimerDelay);
};

function checkVideoLoadingProgressTimerHandler(){
    var progress = getVideoLoadingProgress();
    //TODO update progress bar
    if(progress < 1){
        startCheckVideoLoadingProgressTimer();
    }
};

另外,视频的“预加载”属性值为“auto”并不能保证用户代理将加载整个视频。

var videoElement = null; //TODO set reference to video element

var checkVideoLoadingProgressTimerDelay = 50;
var checkVideoLoadingProgressTimerID = -1;

function getVideoLoadingProgress(){
    var end = 0;
    if(videoElement.buffered.length >= 1){
        end = videoElement.buffered.end(0);
    }
    var progress = end / videoElement.duration;
    progress = isNaN(progress) ? 0 : progress;
    return progress;
};

function startCheckVideoLoadingProgressTimer(){
    checkVideoLoadingProgressTimerID = 
        setTimeout(checkVideoLoadingProgressTimerHandler, checkVideoLoadingProgressTimerDelay);
};

function checkVideoLoadingProgressTimerHandler(){
    var progress = getVideoLoadingProgress();
    //TODO update progress bar
    if(progress < 1){
        startCheckVideoLoadingProgressTimer();
    }
};

Also value "auto" for "preload" attribute of video not guarantee that user agent will load whole video.

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