Skip to content

Instantly share code, notes, and snippets.

@diem1
Created February 16, 2016 16:50
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diem1/4fd37cf1542bb3ab880e to your computer and use it in GitHub Desktop.
Save diem1/4fd37cf1542bb3ab880e to your computer and use it in GitHub Desktop.
Play (mp3) audio file with AudioContext
window.onload = function(){
var context = new AudioContext() || new webkitAudioContext(),
request = new XMLHttpRequest();
request.open("GET", "audio_file.mp3", true);
request.responseType = "arraybuffer";
request.onload = function(){
context.decodeAudioData(request.response, onDecoded);
}
function onDecoded(buffer){
var bufferSource = context.createBufferSource();
bufferSource.buffer = buffer;
bufferSource.connect(context.destination);
bufferSource.start();
}
request.send();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment