【发布时间】:2019-10-18 13:34:59
【问题描述】:
我正在使用 Reactjs 前端开发 VUI 界面。我有一个可以播放的 BLOB 文件,但我想使用 REACT 或 Javascript 将其转换为 .WAV 文件以将其发送到我的服务器。
我尝试了很多东西,但没有找到解决办法
toggleRecording() {
if (this.state.start === 1) {
console.log("we start recording", this.state.start)
this.setState({ start: 0, recognition: "" })
const constraints = {
audio: {
sampleRate: 16000,
channelCount: 1,
}
}
navigator.mediaDevices.getUserMedia(constraints)
.then(stream => {
console.log(this);
this.recorder = new MediaRecorder(stream);
this.recorder.start();
const audioChunks = [];
this.recorder.addEventListener("dataavailable", event => {
audioChunks.push(event.data);
});
this.recorder.addEventListener("stop", () => {
const audioBlob = new Blob(audioChunks, { 'type': 'audio/wav' });
const audioUrl = URL.createObjectURL(audioBlob);
console.log("test: ", audioUrl)
console.log(audioBlob.type)
fetch('http://127.0.0.1:6060/api/sendaudio', {
method: "post",
headers: { 'Content-Type': 'audio/wav' },
body: audioBlob
})
.then(response => {
return response.text()
}).then(text => {
console.log(text);
this.setState({ recognition: text })
});
//to play the audio file:
const audio = new Audio(audioUrl);
audio.play();
});
});
}
我希望获得一个 Wav 文件以发布到我的服务器,但不知道该怎么做....
【问题讨论】:
标签: javascript reactjs voice-recognition voice mediarecorder