-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.js
43 lines (33 loc) · 1.08 KB
/
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
let audioBlobs = [];
let mediaRecorder = null;
let stream = null;
let start = 0;
export async function startRecordingAudio() {
if (!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia)) {
console.log('mediaDevices API or getUserMedia method is not supported in this browser.');
return false;
}
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
audioBlobs = [];
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.addEventListener("dataavailable", event => {
audioBlobs.push(event.data);
});
start = Date.now();
mediaRecorder.start();
return true;
}
export function stopRecordingAudio() {
return new Promise(resolve => {
const mimeType = mediaRecorder.mimeType;
mediaRecorder.addEventListener("stop", () => {
const duration = (Date.now() - start) / 1000;
const audioBlob = new Blob(audioBlobs, { type: mimeType });
resolve({ blob: audioBlob, duration });
});
mediaRecorder.stop();
stream.getTracks().forEach(track => track.stop());
mediaRecorder = null;
stream = null;
});
}