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

后端 未结 4 1567
野趣味
野趣味 2021-02-05 20:17

I\'m building mp3 playlist player with HTML5 and jQuery. At this player there is a horizontal bar grow gradually in conjunction with the buffered present of mp3 file.

he

相关标签:
4条回答
  • 2021-02-05 20:50

    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+"%");
    }
    
    0 讨论(0)
  • 2021-02-05 20:55
    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+"%");
        }
    }
    
    0 讨论(0)
  • 2021-02-05 20:58

    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+"%");
        }
    }
    
    0 讨论(0)
  • 2021-02-05 21:02

    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.

    0 讨论(0)
提交回复
热议问题