我正在尝试将录制的音频保存到服务器上的音频文件中。
参考:http://www.smartjava.org/content/record-audio-using-webrtc-chrome-and-speech-recognition-websockets
代码:
var rec;
function testaudio(){
navigator.getUserMedia({audio: true}, success, error);
function success(stream){
var context = new webkitAudioContext();
console.log(context);
console.log(context.source);
console.log(context.destination);
var mediaStreamSource = context.createMediaStreamSource(stream);
console.log(mediaStreamSource);
rec = new Recorder(mediaStreamSource);
console.log(rec);
}
function error(e){console.log('error in testing audio.. !!1'+e);}
}
function record(){ rec.record();
console.log("recording..");
}
function stopAndSend() {
rec.stop();
console.log("recording stopped!!!");
rec.exportWAV(function(blob){
console.log(blob);
socket.emit('audioSave', {recordedData:blob}); //sending blob to server, socket is created using socket.io
});
}
服务器端代码:
socket.on('audioSave', function(data){
var audio=data.recordedData;
/*
Here i want to save this audio in an audio file , i tried this
fs.writeFile('out.wav', audio, function(err)
{
if(err) {console.log("!!!!!!!!!!!!!!!error in writing file..."+ err);}
console.log(" Audio File created ");
}
);
This method creates text file
*/
});
如何将音频blob写入音频文件?
答案 0 :(得分:2)
您使用的是socket.io吗?我不相信它支持二进制数据,这是你试图发送的。我建议检出BinaryJS,或者如果不起作用,可以通过Base64编码(或其他类似方法)将Blob转换为字符串格式。