Well, playing audio using AudioContext is actually not that difficult.
- Set the context.
- Load any data into the buffer (for example,
FileReader for local files, XHR for deleted files). - Install a new source and run it.
In general, something like this:
var context = new(window.AudioContext || window.webkitAudioContext)(); function playsound(raw) { console.log("now playing a sound, that starts with", new Uint8Array(raw.slice(0, 10))); context.decodeAudioData(raw, function (buffer) { if (!buffer) { console.error("failed to decode:", "buffer null"); return; } var source = context.createBufferSource(); source.buffer = buffer; source.connect(context.destination); source.start(0); console.log("started..."); }, function (error) { console.error("failed to decode:", error); }); } function onfilechange(then, evt) { var reader = new FileReader(); reader.onload = function (e) { console.log(e); then(e.target.result); }; reader.onerror = function (e) { console.error(e); }; reader.readAsArrayBuffer(evt.target.files[0]); } document.getElementById('file') .addEventListener('change', onfilechange.bind(null, playsound), false);
Watch this live jsfiddle , which works for me in Firefox and Chrome.
I chose console.log(new Uint8Array()) for a good estimate, since the browser usually logs the content directly (if the buffer is not huge). For other things you can do with ArrayBuffer s, see, for example, the corresponding MDN documentation .
nmaier
source share