我想使用 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
})