Web Audio API Basic
HTML
</script>
</head>
<body>
<p>Web audio API example: load a sound file and play/stop it on a button click.</p>
<button onclick="initcontext()">Init</button>
<button onclick="loadSound()">Load</button>
<button onclick="playSound()">Play</button>
<button onclick="stopSound()">Stop</button>
</body>
</html>
JavaScript
var context;
var url = "https://ia802508.us.archive.org/5/items/testmp3testfile/mpthreetest.mp3";
var source = null;
var myAudioBuffer = null;
function initcontext() {
try {
context = new AudioContext();
} catch (e) {
alert('Sorry, your browser does not support the Web Audio API.');
}
}
function loadSound() {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
console.log("response is : "+request.response);
console.log("type of response : "+typeof request.response);
request.onload = function () {
context.decodeAudioData(request.response, function (buffer) {
for(var i =0;i<buffer.length;i++)
{
console.log(buffer[i]);
}
myAudioBuffer = buffer;
playSound(myAudioBuffer);
});
}
request.send();
}
function playSound() {
source = context.createBufferSource();
source.buffer = myAudioBuffer;
source.connect(context.destination);
source.start();
}
function stopSound() {
if (source) {
source.stop();
}
}