/ Published in: JavaScript
Code required to play an audio file using Web Audio API
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function playAudio(file, speed){ if (typeof AudioContext == "function") { var audioContext = new AudioContext(); } else if (typeof webkitAudioContext == "function") { var audioContext = new webkitAudioContext(); } var source = audioContext.createBufferSource(); source.connect(audioContext.destination); var xhr = new XMLHttpRequest(); xhr.open("GET", file, true); xhr.responseType = "arraybuffer"; xhr.onload = function() { var buffer = audioContext.createBuffer(xhr.response, false); source.buffer = buffer; source.playbackRate.value = speed; source.noteOn(0); }; xhr.send(); }