Calling the end method on buffered without checking is not reliable. Perhaps you are trying to call the method nothing. Check out this script:
document.querySelector('span').innerHTML = document.querySelector('audio').buffered.length;
<audio src="http://myst729.qiniudn.com/within-temptation_pale.mp3" controls autoplay></audio> <p>Buffered Length: <span></span></p>
Cm? At the first start, the buffered length is 0 โ nothing loaded. You must be sure that the buffered length is not 0 before calling the start or end method.
Every time you read buffered , it is really fixed. Thus, in order to achieve the visual effect of โloadingโ, you need to read it again and again and again.
Here I try to update the downloaded and lost percentage every 50 milliseconds:
var audio = document.querySelector('audio'); var percentages = document.querySelectorAll('span'); function loop() { var buffered = audio.buffered; var loaded; var played; if (buffered.length) { loaded = 100 * buffered.end(0) / audio.duration; played = 100 * audio.currentTime / audio.duration; percentages[0].innerHTML = loaded.toFixed(2); percentages[1].innerHTML = played.toFixed(2); } setTimeout(loop, 50); } loop();
<audio src="http://myst729.qiniudn.com/within-temptation_pale.mp3" controls autoplay></audio> <p>Loaded: <span></span>%</p> <p>Played: <span></span>%</p>
NOTE The MP3 file may not be available in your place. If so, try a different source to your advantage. Otherwise, you will hear very good female vocals and you will see that the percentage changes are constantly, eventually ending at 100%.
source share