Why does audio.buffered.end(0) get an error message when I try to get buffered time

一世执手 提交于 2019-12-03 06:35:03

I believe that error is coming when accessing buffered.end before the element is initialized. you can rewrite that code as to avoid it

track = document.getElementById("music");
track.onprogress = function(){
    var w = 100*(track.buffered.end(0))/track.duration;
    $('#buffered').css("width",w+"%");
}
track = document.getElementById("music");
track.onprogress = function(){
    if(track.buffered.length>0){
        var w = 100*(track.buffered.end(0))/track.duration;
        $('#buffered').css("width",w+"%");
    }
}

The accepted answer doesn't solve the problem for me. You also should check that the track has loaded before you access buffered.end like so:

track.onprogress = function(){
    if (track.readyState === 4){
        var w = 100*(track.buffered.end(0))/track.duration;
        $('#buffered').css("width",w+"%");
    }
}

This error occurs when the audio element is not loaded yet, so there are no time ranges for the code to find so it returns an error.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!