我正在使用RecordRTC.js
将音频发送到后端。但我无法做到这一点。甚至找不到原因。
我的代码:
//捕获麦克风
captureMicrophone(callback) {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(callback)
.catch( (error)=> {
alert('Unable to access your microphone.');
console.error(error);
});
}
//开始录制
startRecording () {
this.captureMicrophone( (microphone) => {
let audio = document.querySelector('audio');
window['setSrcObject'](microphone, audio);
audio.play();
this.recorder = window['RecordRTC'](microphone, {
type: 'audio',
recorderType: window['StereoAudioRecorder'],
desiredSampRate: 16000
});
this.recorder.startRecording();
// release microphone on stopRecording
this.recorder.microphone = microphone;
(<HTMLInputElement> document.getElementById('btn-stop-recording')).disabled = false;
});
};
//停止录制
stopRecording () {
(<HTMLInputElement> document.getElementById('btn-stop-recording')).disabled = false;
this.recorder.stopRecording(() => {
this.audio = document.querySelector('audio');
var blob = this.recorder.getBlob();
this.audio.src = URL.createObjectURL(blob);
this.audio.play();
this.recorder.microphone.stop();
});
};
//这是将数据发送到HTTP服务以调用后端
sendMMS() {
var fileType = 'audio'; // or "audio"
var fileName = 'abcde.wav'; // or "wav"
let formData = new FormData();
formData.append('filename', fileName);
formData.append('data', this.audio.src);
this.bsService.sendMMS(formData);
}
电话正在进行中。但在后端,null将作为数据出现。我认为附加数据有问题。我从StackOverflow尝试了很多解决方案。但是,没有成功。
请帮我。
谢谢...
请试试这个:
sendMMS() {
var fileType = 'audio'; // or "audio"
var fileName = 'abcde.wav'; // or "wav"
let formData = new FormData();
formData.append('filename', fileName);
formData.append('data', this.recorder.getBlob()); // --------- check this line
this.bsService.sendMMS(formData);
}