Created
October 21, 2013 17:00
-
-
Save fnobi/7087203 to your computer and use it in GitHub Desktop.
WebAudioAPIで遊べるようになった ref: http://qiita.com/fnobi/items/2f08a67800dec1d61f21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var context = new window.AudioContext(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ここに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; | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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