Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preload multiple audio files

Tags:

html

I have a single audio control on my webpage. I wish to use it to play multiple very short audio files depending on the state of the page. I do not wish to load the files as I play them. How do I load all of these files on page load?

Here's is a rough idea of what I'm doing:

http://jsfiddle.net/L0c9ccx9/20/

audio.src = ...; audio.load(); audio.play(); 
like image 455
Baz Avatar asked Jun 25 '15 20:06

Baz


People also ask

How do I preload an audio file?

We use preload=”auto” attribute to preload the audio file. The HTML Audio Preload Attribute is used to specify how the author thinks the audio should be loaded when the page loads. The audio preload attribute allows the author to indicate to the browser how the user experience of a website should be implemented.

What is the use of preload auto attribute in audio?

The preload attribute specifies if and how the author thinks that the audio file should be loaded when the page loads. The preload attribute allows the author to provide a hint to the browser about what he/she thinks will lead to the best user experience. This attribute may be ignored in some instances.

Which attribute enables automatic buffering of an audio file in html5?

autobuffer: If this attribute is used, the audio will begin buffering automtically, even if the playback isn't automatic. controls: This attribute allows the user to control audio playback, volume, seeking and pause or resume playback.


1 Answers

var audioFiles = [      "http://www.teanglann.ie/CanC/nua.mp3",      "http://www.teanglann.ie/CanC/ag.mp3",      "http://www.teanglann.ie/CanC/dul.mp3",      "http://www.teanglann.ie/CanC/freisin.mp3"  ];        function preloadAudio(url) {      var audio = new Audio();      // once this file loads, it will call loadedAudio()      // the file will be kept by the browser as cache      audio.addEventListener('canplaythrough', loadedAudio, false);      audio.src = url;  }        var loaded = 0;  function loadedAudio() {      // this will be called every time an audio file is loaded      // we keep track of the loaded files vs the requested files      loaded++;      if (loaded == audioFiles.length){      	// all have loaded      	init();      }  }        var player = document.getElementById('player');  function play(index) {      player.src = audioFiles[index];      player.play();  }        function init() {      // do your stuff here, audio has been loaded      // for example, play all files one after the other      var i = 0;      // once the player ends, play the next one      player.onended = function() {      	i++;          if (i >= audioFiles.length) {              // end               return;          }      	play(i);      };      // play the first file      play(i);  }        // we start preloading all the audio files  for (var i in audioFiles) {      preloadAudio(audioFiles[i]);  }
<audio id="player"></audio>
like image 54
Juank Avatar answered Sep 22 '22 23:09

Juank