Created
June 3, 2020 17:16
-
-
Save 505e06b2/300bc7420f01976a32b58a966c7b3805 to your computer and use it in GitHub Desktop.
Gapless HTML5 Audio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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