【问题标题】:chrome html5 video buffered.end eventchrome html5 视频 buffered.end 事件
【发布时间】:2011-12-05 17:06:03
【问题描述】:

我正在尝试检测视频文件何时完成加载。我让它在 Firefox 和 safari 上成功运行,但在 chrome 上,缓冲事件的行为很奇怪.. 所以, 在我的本地主机 chrome 中工作正常,但是当我上传到服务器时;

  1. 缓冲百分比在 %50 左右停止,但缓冲 %100,

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

这是我的 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();
            });

有什么想法吗? 谢谢

【问题讨论】:

    标签: javascript html google-chrome video buffer


    【解决方案1】:

    试试这个:

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

    ... cmets 从 mediaelement.js 复制,代码也是如此,但经过调整以便在此处显示。我省略了 Firefox 3.0 的代码,因为它不太相关。 在所有当前浏览器中都能正常工作

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

    【讨论】:

    • 您的评论说在 IE 7/8 中测试,但我想测试会失败,因为那些浏览器不使用 addEventListener
    • @AlienWebguy:真丢脸——这是真的!我省略了myVideoTag.attachEvent('progress',function(e){...}); 的额外例程
    猜你喜欢
    • 2013-09-03
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2011-08-23
    • 2013-05-22
    相关资源
    最近更新 更多