Created
February 16, 2016 16:50
-
-
Save diem1/4fd37cf1542bb3ab880e to your computer and use it in GitHub Desktop.
Play (mp3) audio file with AudioContext
This file contains 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
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