I want to stop record audio from microphone when silence is detected after 2 seconds,
there is a only function to stop record on event.
But how can I detect a silence for 2 seconds and stop recording?
const downloadLink = document.getElementById('download');
const stopButton = document.getElementById('stop');
const handleSuccess = function (stream) {
const options = {mimeType: 'audio/webm'};
const recordedChunks = [];
const mediaRecorder = new MediaRecorder(stream, options);
mediaRecorder.addEventListener('dataavailable', function (e) {
if (e.data.size > 0) recordedChunks.push(e.data);
});
// Deve stoppare il record quando c'è silenzio
mediaRecorder.addEventListener('stop', function () {
$('#microfono-img').removeClass('mic_active');
downloadLink.href = URL.createObjectURL(new Blob(recordedChunks));
downloadLink.download = 'acetest.wav';
});
stopButton.addEventListener('click', function () {
mediaRecorder.stop();
});
mediaRecorder.start();
};
navigator.mediaDevices.getUserMedia({audio: true, video: false})
.then(handleSuccess);