getUserMedia を使用するとデバイスにアクセスできますが、オーディオを録音するのはユーザー次第です。そのためには、デバイスを「リッスン」して、データのバッファーを構築する必要があります。次に、デバイスの再生を停止すると、そのデータを WAV ファイル (またはその他の形式) としてフォーマットできます。フォーマットしたら、サーバー、S3 にアップロードするか、ブラウザで直接再生できます。
バッファの構築に役立つ方法でデータをリッスンするには、ScriptProcessorNode が必要です。ScriptProcessorNode は、基本的に入力 (マイク) と出力 (スピーカー) の間に位置し、ストリーミング中にオーディオ データを操作する機会を提供します。残念ながら、実装は簡単ではありません。
あなたは必要になるでしょう:
すべてを一緒に入れて:
navigator.getUserMedia({audio: true},
function(stream) {
// create the MediaStreamAudioSourceNode
var context = new AudioContext();
var source = context.createMediaStreamSource(stream);
var recLength = 0,
recBuffersL = [],
recBuffersR = [];
// create a ScriptProcessorNode
if(!context.createScriptProcessor){
node = context.createJavaScriptNode(4096, 2, 2);
} else {
node = context.createScriptProcessor(4096, 2, 2);
}
// listen to the audio data, and record into the buffer
node.onaudioprocess = function(e){
recBuffersL.push(e.inputBuffer.getChannelData(0));
recBuffersR.push(e.inputBuffer.getChannelData(1));
recLength += e.inputBuffer.getChannelData(0).length;
}
// connect the ScriptProcessorNode with the input audio
source.connect(node);
// if the ScriptProcessorNode is not connected to an output the "onaudioprocess" event is not triggered in chrome
node.connect(context.destination);
},
function(e) {
// do something about errors
});
このすべてを自分で構築するよりも、すばらしい AudioRecorderコードを使用することをお勧めします。また、WAV ファイルへのバッファの書き込みも処理します。 ここにデモがあります。
別の優れたリソースを次に示します。