Html5 Audio Looping

gapless looping audio html5

Unfortunately this is one of the weaknesses of the HTML5 element. There is no guarantee that audio will play when you want it to or without delay.

There are two options worth looking into:

  1. SoundManager2 - a great library that would probably be helpful here, though it'll use Flash to play the audio in this case;

  2. Web Audio API in Chrome or the Audio Data API in Firefox - both are really new, and not really ready for prime time yet, but allow you to do things like scheduled audio playback, looping and a whole lot more.

HTML5 Audio Looping

While loop is specified, it is not implemented in any browser I am aware of Firefox [thanks Anurag for pointing this out]. Here is an alternate way of looping that should work in HTML5 capable browsers:

var myAudio = new Audio('someSound.ogg'); 
myAudio.addEventListener('ended', function() {
this.currentTime = 0;
this.play();
}, false);
myAudio.play();

Why won't my HTML5 audio loop?

You can just do this

<audio autoplay loop>

The attributes do not need to have anything else.


EDIT

According to this, Firefox doesn't like loop. It suggests a js solution:

document.getElementById('audio_2').addEventListener('ended', function(){
this.currentTime = 0;
}, false);

http://forestmist.org/2010/04/html5-audio-loops/


EDIT

HTML5 audio loop now works with firefox. Confirmed in version 26.0 (might have been earlier)

Javascript audio loop

You could try using onended:
Reference: http://www.w3schools.com/tags/av_event_ended.asp or it could be you need to increment your count before you call play() again

var count = 1;
var audio = document.getElementById('beep');
audio.onended = function() {
if(count <= 3){
count++;
this.play();
}
};

Create Seamless Loop of Audio - Web

You can use the Web Audio API instead. There are a couple of caveats with this, but it will allow you to loop accurately down to the single sample level.

The caveats are that you have to load the entire file into memory. This may not be practical with large files. If the files are only a few seconds it should however not be any problem.

The second is that you have to write control buttons manually (if needed) as the API has a low-level approach. This means play, pause/stop, mute, volume etc. Scanning and possibly pausing can be a challenge of their own.

And lastly, not all browsers support Web Audio API - in this case you will have to fallback to the regular Audio API or even Flash, but if your target is modern browsers this should not be a major problem nowadays.

Example

This will load a 4 bar drum-loop and play without any gap when looped. The main steps are:

  • It loads the audio from a CORS enabled source (this is important, either use the same domain as your page or set up the external server to allow for cross-origin usage as Dropbox does for us in this example).
  • AudioContext then decodes the loaded file
  • The decoded file is used for the source node
  • The source node is connected to an output
  • Looping is enabled and the buffer is played from memory.

var actx = new (AudioContext || webkitAudioContext)(),
src = "https://dl.dropboxusercontent.com/s/fdcf2lwsa748qav/drum44.wav",
audioData, srcNode; // global so we can access them from handlers

// Load some audio (CORS need to be allowed or we won't be able to decode the data)
fetch(src, {mode: "cors"}).then(function(resp) {return resp.arrayBuffer()}).then(decode);

// Decode the audio file, then start the show
function decode(buffer) {
actx.decodeAudioData(buffer, playLoop);
}

// Sets up a new source node as needed as stopping will render current invalid
function playLoop(abuffer) {
if (!audioData) audioData = abuffer; // create a reference for control buttons
srcNode = actx.createBufferSource(); // create audio source
srcNode.buffer = abuffer; // use decoded buffer
srcNode.connect(actx.destination); // create output
srcNode.loop = true; // takes care of perfect looping
srcNode.start(); // play...
}

// Simple example control
document.querySelector("button").onclick = function() {
if (srcNode) {
srcNode.stop();
srcNode = null;
this.innerText = "Play";
} else {
playLoop(audioData);
this.innerText = "Stop";
}
};
<button>Stop</button>

Audio html5 loop pause

You need to set the delay to 20 minutes

var audioPlayer = this;
setTimeout(function() {
audioPlayer.play();
}, 10*60*1000)

also you can remove this

<audio id="war_soundy_audio_player" preload="auto" ' . $auto_play . ' ' . $audio_loop . '>' .

If you want it to start on load add autoplay in the audio tag ,also add 10*60*1000 add the time that is spent the the audio is player for example if audio is 5 minutes change to 15*60*1000

html5 play audio loop 4x then stop

I managed to do it like this:

 $('#play_audio').click(function () {                     
var times = 4;
var loop = setInterval(repeat, 500);

function repeat() {
times--;
if (times === 0) {
clearInterval(loop);
}

var audio = document.createElement("audio");
audio.src = "files/windows%20default.mp3";

audio.play();
}

repeat();
});
});


Related Topics



Leave a reply



Submit