Skip to content

Instantly share code, notes, and snippets.

@505e06b2
Created June 3, 2020 17:16
Show Gist options
  • Save 505e06b2/300bc7420f01976a32b58a966c7b3805 to your computer and use it in GitHub Desktop.
Save 505e06b2/300bc7420f01976a32b58a966c7b3805 to your computer and use it in GitHub Desktop.
Gapless HTML5 Audio
function gaplessAudio() {
const audio = new AudioContext();
let buffer_source;
this.load = async (url) => {
const file_contents = await (await fetch(url)).arrayBuffer();
audio.decodeAudioData(file_contents, function(buffer) {
if(buffer_source) buffer_source.disconnect();
buffer_source = audio.createBufferSource();
buffer_source.buffer = buffer;
buffer_source.connect(audio.destination);
buffer_source.loop = true;
buffer_source.start(0);
}, function(e) {
console.log("Error with decoding audio data" + e.err);
});
};
this.suspend = async () => {await audio.suspend();};
this.resume = async () => {await audio.resume();};
this.state = () => {return audio.state;};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment