javascript chrome html5 视频 buffered.end 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8364313/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:12:40  来源:igfitidea点击:

chrome html5 video buffered.end event

javascripthtmlgoogle-chromevideobuffer

提问by Bahad?r

I'm trying to detect when a video file has completed loading. i made it work successfully on firefox and safari but on chrome, buffered event behaves strange.. so, in my local host chrome works fine but when i upload to server;

我正在尝试检测视频文件何时完成加载。我让它在 firefox 和 safari 上成功运行,但在 chrome 上,缓冲事件的行为很奇怪。

  1. buffer percentage stops about %50 but buffers %100,

  2. when page refreshed, percentage stay at %0 but it continues to buffering..

  1. 缓冲百分比停止约 %50,但缓冲 %100,

  2. 当页面刷新时,百分比保持在 %0 但它继续缓冲..

here is my javascript

这是我的 javascript

function loaded()
        {
            var v = document.getElementById('myVideo');
            var r = v.buffered;
            var total = v.duration;
            var current=v.currentTime;
            var start = r.start(0);
                    var end = r.end(0); 
            var downloadPercent= Math.round((end / total)*100)
            $("#loadProgress").css('width',downloadPercent+ '%');

                    if(downloadPercent==100){
                $("#preloaderWrapper").fadeOut(function(){
                document.getElementById('myVideo').play();
                clearInterval(ratoteLoad);
                $(this).remove();                   
                    });             
            }       

        }   

            $('#myVideo').bind('progress', function() 
            {
                loaded();
            });

any idea? thank you

任何的想法?谢谢

回答by J?rn Berkefeld

try this instead:

试试这个:

myVideoTag = document.getElementById('video');
myVideoTag.addEventListener('progress', function(e) {
    var percent = null;
    // FF4+, Chrome
    if (myVideoTag && myVideoTag.buffered && myVideoTag.buffered.length > 0 && myVideoTag.buffered.end && myVideoTag.duration) {
        percent = myVideoTag.buffered.end(0) / myVideoTag.duration;
    } 
    // Some browsers (e.g., FF3.6 and Safari 5) cannot calculate target.bufferered.end()
    // to be anything other than 0. If the byte count is available we use this instead.
    // Browsers that support the else if do not seem to have the bufferedBytes value and
    // should skip to there. Tested in Safari 5, Webkit head, FF3.6, Chrome 6, IE 7/8.
    else if (myVideoTag && myVideoTag.bytesTotal != undefined && myVideoTag.bytesTotal > 0 && myVideoTag.bufferedBytes != undefined) {
        percent = myVideoTag.bufferedBytes / myVideoTag.bytesTotal;
    }

    if (percent !== null) {
        percent = 100 * Math.min(1, Math.max(0, percent));

        // ... do something with var percent here (e.g. update the progress bar)

    }

}, false);

... comments copied from mediaelement.js, code as well but adjusted for easier display here. I omitted the code for Firefox 3.0 as it's less than relevant. working fine in all current browsers

... 从 mediaelement.js 复制的评论,代码也是如此,但为了更容易在此处显示而进行了调整。我省略了 Firefox 3.0 的代码,因为它不太相关。在所有当前浏览器中都能正常工作

PS: thx to John Dyer for mejs - great stuff ;)

PS:感谢 John Dyer for mejs - 很棒的东西;)