【发布时间】:2017-01-16 13:43:32
【问题描述】:
我正在尝试使用 Web API Media Recorder 在浏览器中录制用户的声音。
在这个阶段,我想要对录制的音频做的只是将它添加到音频元素的源中并播放它。
当我停止录制时,触发了'ondataavailable'事件,但数据大小为0,无法播放任何内容。
这是我的代码所在的位置。我正在使用反应。任何想法将不胜感激!
handleRecording (stream) {
const recordedChunks = this.state.recordedChunks;
const mediaRecorder = new MediaRecorder(stream)
const _this = this;
mediaRecorder.start();
mediaRecorder.ondataavailable = function(e) {
if (e.data.size > 0){
recordedChunks.push(e.data);
_this.setState({recordedChunks:recordedChunks});
}
}
mediaRecorder.onstop = function(e) {
if (recordedChunks){
if(recordedChunks.length > 0) {
const audio = document.querySelector('audio');
audio.controls = true;
const blob = new Blob(recordedChunks, { 'type' : 'audio/ogg; codecs=opus' });
const audioURL = window.URL.createObjectURL(blob);
audio.src = audioURL;
}
}
}
this.setState({mediaRecorder:mediaRecorder});
},
startRecording () {
const recording = true;
this.setState({recording:recording});
navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(this.handleRecording);
},
stopRecording () {
const recording = false;
const mediaRecorder = this.state.mediaRecorder;
mediaRecorder.stop();
this.setState({recording:recording, mediaRecorder:mediaRecorder});
}
文档链接:https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/ondataavailable
【问题讨论】:
标签: javascript html5-audio audio-recording mediarecorder