我想使用 javascript 录制一些音频。我找到了一些解释,但对我来说不起作用。据我了解,下面的代码stop
应该触发addEventListener('dataavailable' ...
。但据我所知,此代码从未执行过。有人能告诉我我必须更改什么才能eventlistener
执行吗?
var demo = document.getElementById("demo")
const startButton = document.getElementById('start');
const stopButton = document.getElementById('stop');
let media_recorder
let audioChunks = []
// https://developers.deepgram.com/docs/getting-started-with-pre-recorded-audio
// https://www.tutorialspoint.com/how-to-record-and-play-audio-in-javascript
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
demo.innerHTML += "demo"
// https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/dataavailable_event
media_recorder = new MediaRecorder(stream);
media_recorder.addEventListener('dataavailable', e => {
audioChunks = []
audioChunks.push(e.data);
});
startButton.addEventListener('click', () => {
// audioChunks = [];
media_recorder.start();
demo.innerHTML += 'Recording started! Speak now.';
});
stopButton.onclick = () => {
media_recorder.stop();
demo.innerHTML += "stop "
audioChunks = []
demo.innerHTML += "Recording stoped!"
};
}).catch (err => {
demo.innerHTML += err
})
我认为不必要的清除
audioChunks
导致了这个问题。这是我的尝试;希望它能有所帮助。我还实现了一个音频控制来显示它正在工作,尽管由于 Stack Overflow 上的安全原因它不起作用,你应该在自己的本地主机上尝试您必须在 media_recorder 中输入收集音频数据的毫秒数,例如
媒体记录器.启动(200);