Skip to content

Instantly share code, notes, and snippets.

@polaris
Created January 9, 2012 12:42
Show Gist options
  • Save polaris/1582778 to your computer and use it in GitHub Desktop.
Save polaris/1582778 to your computer and use it in GitHub Desktop.
Client
var ctx = new webkitAudioContext();
var startTime = 0;
function playSound(buffer) {
var src = ctx.createBufferSource();
src.buffer = buffer;
src.looping = false;
src.connect(ctx.destination);
src.noteOn(startTime);
startTime += src.buffer.duration;
}
var ws = new WebSocket('ws://localhost:8080/data');
ws.binaryType = 'arraybuffer';
ws.onmessage = function(e) {
console.log('About to decode ' + e.data.byteLength + ' bytes MP3 data');
ctx.decodeAudioData(e.data, function (buffer) {
console.log('Finished decoding');
playSound(buffer);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment