-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.js
More file actions
118 lines (99 loc) · 3.28 KB
/
audio.js
File metadata and controls
118 lines (99 loc) · 3.28 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const recordButton = document.querySelector("#record");
const stopButton = document.querySelector("#stop");
let recorder, streams, start;
const fixDuration = (blob, duration) =>
new Promise((resolve) =>
ysFixWebmDuration(blob, duration, (result) => resolve(result))
);
function createRecorder(stream) {
const recorder = new MediaRecorder(stream, {
mimeType: "audio/webm",
audioBitsPerSecond : 12000,
});
const chunks = [];
recorder.ondataavailable = (e) => chunks.push(e.data);
recorder.onstop = async function () {
const blob = new Blob(chunks, { type: "audio/webm" });
const fixedBlob = await fixDuration(blob, Date.now() - start);
const videoURL = window.URL.createObjectURL(fixedBlob);
const downloadLink = window.document.createElement("a");
document.body.appendChild(downloadLink);
downloadLink.href = videoURL;
const name =
new Date()
.toISOString()
.replace(/[^0-9]/g, "")
.substr(0, 14) + ".webm";
downloadLink.setAttribute("download", name);
downloadLink.setAttribute("name", name);
downloadLink.click();
window.URL.revokeObjectURL(videoURL);
downloadLink.remove();
};
recorder.start();
return recorder;
}
async function onRecordClicked() {
try {
if (
window.MediaRecorder == undefined ||
!navigator.mediaDevices.getDisplayMedia
) {
alert("Your browser is not supported.");
return false;
}
if (
(await navigator.mediaDevices.enumerateDevices()).filter(
(device) => device.kind == "audioinput"
).length == 0
) {
alert("No audio devices found.");
return false;
}
const screenStream = await navigator.mediaDevices.getDisplayMedia({
video: true,
audio: true,
});
const micStream = await navigator.mediaDevices.getUserMedia({
audio: true,
});
const composedStream = new MediaStream();
if (screenStream.getAudioTracks().length > 0) {
const context = new AudioContext();
const audioDestination = context.createMediaStreamDestination()
context.createMediaStreamSource(screenStream).connect(audioDestination);
if (micStream && micStream.getAudioTracks().length > 0) {
context.createMediaStreamSource(micStream).connect(audioDestination);
} else {
alert("Please connect a microphone.");
return false;
}
for (const audioTrack of audioDestination.stream.getAudioTracks()) {
composedStream.addTrack(audioTrack);
}
} else {
alert('Please check "Share audio" in the "Choose what to share" dialog. If you can\'t see it, select the tab you want to record first.');
return false;
}
recorder = createRecorder(composedStream);
screenStream.getVideoTracks()[0].addEventListener("ended", onStopClicked);
streams = [composedStream, screenStream, micStream];
recordButton.disabled = true;
stopButton.disabled = false;
start = Date.now();
return true;
} catch (e) {
alert("Could not start recording: " + e.message.toLowerCase() + ".");
return fal
}
}
function onStopClicked() {
recorder.stop();
for (const stream of streams) {
for (const track of stream.getTracks()) {
track.stop();
}
}
recordButton.disabled = false;
stopButton.disabled = true;
}