Web Audio Basics (Concise)
by Aqilah Misuary
HTML
<button id="playSoundBtn">Play</button>
<button id="stopSoundBtn">Stop</button>
JavaScript
var context = new (window.AudioContext || window.webkitAudioContext)();
var source;
var songLength;
var url = "https://dl.dropboxusercontent.com/u/30075450/kick_deep.mp3";
var myAudioBuffer;
var play = document.getElementById("playSoundBtn");
var stop = document.getElementById("stopSoundBtn");
(function(source) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function () {
context.decodeAudioData(request.response, function (buffer) {
myAudioBuffer = buffer;
});
}
request.send();
}());
play.onclick = function(source) {
source = context.createBufferSource();
source.buffer = myAudioBuffer;
source.start(context.currentTime);
source.stop(context.currentTime + 0.5);
//source.stop(context.currentTime + 5);
source.connect(context.destination);
// play.setAttribute('disabled', 'disabled');
}
// stop.onclick = function() {
// play.removeAttribute('disabled');
//}