Gapless Looping Audio HTML5

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.

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>

How can I do gapless audio looping with mobile browser?

With HTML5
If you are using HTML5, then use loop attribute.

<audio controls loop>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>

It doesn't create gap, check with your audio file, most of the audio file has gap at the end.

You can test it here, just add loop attribute and run the page.

With JavaScript

Here is also an alternative by using javascript

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

Here JavaScript will create little gap, you can overcome it by playing loop not when audio is finished but when audio is about to finish.
Here is code.

Here is what you want.

myAudio = new Audio('http://unska.com/audio/pinknoise.ogg'); 
myAudio.ontimeupdate= function(i) {
if((this.currentTime / this.duration)>0.9){
this.currentTime = 0;
this.play();
}
};
myAudio.play();

Here is Demo.

How can I remove the gap when the song replays in the html audio tag?

This actually is a problem by the audio tag. You can however try to minimise the effect by doing something like this:

var audio = new Audio('http://www.phatdrumloops.com/audio/wav/ifineededs.wav')
audio.addEventListener('timeupdate', function(){
var buffer = .1
if(this.currentTime > this.duration - buffer){
this.currentTime = 0
this.play()
}}, false);
audio.play()

Edit: According to your comments, I've digged a bit deeper. This is what I found:
https://github.com/Hivenfour/SeamlessLoop

Problem here is, you need to provide the correct length of the track in ms (the track you want to loop)

See a fiddle here.
https://jsbin.com/simuvoduhi/edit?html,js,output

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();


Related Topics



Leave a reply



Submit