4

我正在尝试将录制的音频保存到服务器上的音频文件中。

参考: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);}   

}

函数记录(){rec.record();

console.log("recording..");

}

函数 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 写入音频文件?

4

1 回答 1

2

你在使用 socket.io 吗?我不相信它支持二进制数据,这是你试图发送的。我建议检查BinaryJS,或者如果这不起作用,则通过 Base64 编码(或其他类似方法)将 Blob 转换为字符串格式。

于 2012-12-31T21:52:29.333 回答