无法使用“RecordRTC.js”将音频发送到后端
Cannot send audio using `RecordRTC.js` to backend
我正在使用 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 作为数据出现。我认为附加数据有问题。我尝试了很多来自 Whosebug 的解决方案。但是,没有成功。
请帮帮我。
谢谢...
请试试这个:
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);
}
我正在使用 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 作为数据出现。我认为附加数据有问题。我尝试了很多来自 Whosebug 的解决方案。但是,没有成功。
请帮帮我。
谢谢...
请试试这个:
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);
}