Skip to content

Instantly share code, notes, and snippets.

@fnobi
Created October 21, 2013 17:00
Show Gist options
  • Save fnobi/7087203 to your computer and use it in GitHub Desktop.
Save fnobi/7087203 to your computer and use it in GitHub Desktop.
WebAudioAPIで遊べるようになった ref: http://qiita.com/fnobi/items/2f08a67800dec1d61f21
var context = new window.AudioContext();
// ここにbufferを格納したい
var buffer;
// ファイルを取得 (arraybufferとして)
var request = new XMLHttpRequest();
request.open('GET', '/sounds/sample.wav', true);
request.responseType = 'arraybuffer';
request.send();
request.onload = function () {
// 読み込みが終わったら、decodeしてbufferにいれておく
var res = request.responce;
context.decodeAudioData(res, function (buf) {
buffer = buf;
});
};
// source作成
var source = context.createBufferSource();
// さきほど作ったbufferを、ここにセット
source.buffer = buffer;
// 再生準備。contextの持っている再生先に接続
source.connect(context.destination);
// 再生 (引数はtimeout)
source.noteOn(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment