Your Very First Web Audio API
Eiji Kitamura (Google Developer Advocate)
Eiji Kitamura (Google Developer Advocate)



var c = new webkitAudioContext(); var source = c.createBufferSource(); // Generate BufferSourceNode source.buffer = buffer; // Pre-loaded audio file source.connect(c.destination); // Connect nodes source.noteOn(0); // Play sound
var g = c.createGainNode(); // Generate GainNode g.gain.value = 0.5; // Turn down volume to 0.5 source.connect(g); // Connect sound source to GainNode g.connect(c.destination); // Connect GainNode to destination
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer'; // Load binary (arraybuffer)
// Decode loaded audio binary file asynchronously
xhr.onload = function() {
context.decodeAudioData(xhr.response, function(buffer) {
dogBarkingBuffer = buffer;
}, onError);
}
xhr.send();