-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaudio-recorder.js
57 lines (46 loc) · 1.7 KB
/
audio-recorder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// audio-recorder.js
// Get references to DOM elements
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const audioPlayback = document.getElementById('audioPlayback');
const downloadLink = document.getElementById('downloadLink');
let mediaRecorder;
let recordedChunks = [];
// Function to start recording
startBtn.addEventListener('click', async () => {
// Request microphone access
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
// Create a new MediaRecorder instance
mediaRecorder = new MediaRecorder(stream);
// Event handler for data available
mediaRecorder.ondataavailable = (event) => {
if (event.data.size > 0) {
recordedChunks.push(event.data);
}
};
// Event handler for when recording stops
mediaRecorder.onstop = () => {
const blob = new Blob(recordedChunks, { type: 'audio/webm' });
const audioURL = URL.createObjectURL(blob);
// Set the audio source to the recorded data
audioPlayback.src = audioURL;
audioPlayback.play();
// Prepare the download link
downloadLink.href = audioURL;
downloadLink.download = 'recording.webm';
downloadLink.style.display = 'block';
downloadLink.innerText = 'Download Recording';
// Reset recorded chunks for the next recording
recordedChunks = [];
};
// Start recording
mediaRecorder.start();
startBtn.disabled = true;
stopBtn.disabled = false;
});
// Function to stop recording
stopBtn.addEventListener('click', () => {
mediaRecorder.stop();
startBtn.disabled = false;
stopBtn.disabled = true;
});