Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a loading bar for an HTML5 audio element?

I'm trying to make a loading bar (showing percentage loaded/buffered) for an HTML5 audio element.

For the video tag it's possible to calculate using the following:

video.buffered.end(0) / video.duration

But I can't get this to work with the audio tag. It just returns a fix value.

Any idea?

Thanks!

like image 984
user1767586 Avatar asked Oct 23 '12 08:10

user1767586


People also ask

What is the correct HTML5 element for playing audio files?

The “source” element is used to specify the audio files which the browser may use.

How do I embed audio in HTML5?

To embed audio in HTML, we use the <audio> tag. Before HTML5, audio cannot be added to web pages in the Internet Explorer era. To play audio, we used web plugins like Flash. After the release of HTML5, it is possible.

How do you customize audio controls in HTML?

HTML 5 audio tags can be styled. By using the audio tag with “controls” attribute, the default browsers player is used. You can customize by not using the browsers controls. You can also add CSS classes to each one of the elements and style them accordingly.


2 Answers

Calling end method on buffered without checking is unreliable. It's possible you're trying to call the method on nothing. Check this fiddle:

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>

See? At the very first beginning, buffered length is 0 - nothing has loaded. You need to be sure that buffered length is not 0 before calling start or end method.


Everytime you read buffered, it is fixed indeed. So, to achive a visually "loading" effect, you need to read it again and again and again.

Here I try to update the loaded and played percentage every 50 millisecond:

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 accessible in your place. If that's the case, just try another source at your favor. Otherwise you will hear a very nice female vocal, and see the percentage changes continously, eventually ends up 100%.

like image 128
Leo Avatar answered Oct 25 '22 22:10

Leo


I am not quite sure if i do undestand your prob. but here is a way i used to calculate how much audio is buffered

var audio = document.querySelector('audio');
var set;
window.onload = function(){set=setInterval(buffer,1000);}; 
    function buffer () {
    if(audio.buffered.length>0){
   var percent = (audio.buffered.end(0) / audio.duration) * 100;
       document.querySelector('p').innerHTML = percent+'%';
     if(percent === 100){
           clearInterval(set); 
       } 
       }
}
<audio src="http://customhtml5video.000webhostapp.com/audio.mp3" controls></audio>
<p></p>
like image 24
Legendary Power Avatar answered Oct 25 '22 20:10

Legendary Power