-
Notifications
You must be signed in to change notification settings - Fork 0
/
script2.js
71 lines (64 loc) · 2.05 KB
/
script2.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
let btn = document.querySelector('.record-btn');
const handleRecord = function ({ stream, mimeType }) {
// to collect stream chunks
let recordedChunks = [];
stopped = false;
const mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = function (e) {
if (e.data.size > 0) {
recordedChunks.push(e.data);
}
// shouldStop => forceStop by user
if (shouldStop === true && stopped === false) {
mediaRecorder.stop();
stopped = true;
}
};
mediaRecorder.onstop = function () {
const blob = new Blob(recordedChunks, {
type: mimeType,
});
recordedChunks = [];
const filename = window.prompt('Enter file name'); // input filename from user for download
downloadLink.href = URL.createObjectURL(blob); // create download link for the file
downloadLink.download = `${filename}.webm`; // naming the file with user provided name
stopRecord();
};
mediaRecorder.start(200); // here 200ms is interval of chunk collection
};
async function recordAudio() {
const mimeType = 'audio/webm';
shouldStop = false;
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
handleRecord({ stream, mimeType });
}
async function recordVideo() {
const mimeType = 'video/webm';
shouldStop = false;
const constraints = {
audio: true,
video: true,
};
const stream = await navigator.mediaDevices.getUserMedia(constraints);
handleRecord({ stream, mimeType });
}
async function recordScreen() {
const mimeType = 'video/webm';
shouldStop = false;
const constraints = {
video: true,
};
const displayStream = await navigator.mediaDevices.getDisplayMedia({
video: true,
audio: true,
});
// voiceStream for recording voice with screen recording
const voiceStream = await navigator.mediaDevices.getUserMedia({
audio: true,
video: false,
});
let tracks = [...displayStream.getTracks(), ...voiceStream.getAudioTracks()];
const stream = new MediaStream(tracks);
handleRecord({ stream, mimeType });
}
btn.addEventListener('click', recordScreen);