-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
161 lines (135 loc) · 4.55 KB
/
Copy pathapp.js
File metadata and controls
161 lines (135 loc) · 4.55 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Welcome to app.js!
let shouldStop = false;
let stopped = false;
const videoElement = document.getElementsByTagName('video')[0];
const downloadLink = document.getElementById('download');
const stopButton = document.getElementById('stop');
// Modal elements
const modal = document.getElementById('filename-modal');
const closeModal = document.querySelector('.modal .close');
const saveFilenameButton = document.getElementById('save-filename');
const filenameInput = document.getElementById('filename-input');
// Show the modal
function showModal(callback) {
modal.style.display = 'block';
saveFilenameButton.onclick = function () {
const filename = filenameInput.value.trim();
if (filename) {
callback(filename);
modal.style.display = 'none';
}
};
}
// Close the modal
closeModal.onclick = function () {
modal.style.display = 'none';
};
// Click outside of modal closes it
window.onclick = function (event) {
if (event.target === modal) {
modal.style.display = 'none';
}
};
function startRecord() {
$('.btn-info').prop('disabled', true);
$('#stop').prop('disabled', false);
$('#download').css('display', 'none');
}
function stopRecord() {
$('.btn-info').prop('disabled', false);
$('#stop').prop('disabled', true);
$('#download').css('display', 'block');
}
const audioRecordConstraints = {
echoCancellation: true
};
stopButton.addEventListener('click', function() {
shouldStop = true;
});
const handleRecord = function({stream, mimeType}) {
startRecord();
let recordedChunks = [];
stopped = false;
const mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = function(e) {
if (e.data.size > 0) {
recordedChunks.push(e.data);
}
if (shouldStop === true && stopped === false) {
mediaRecorder.stop();
stopped = true;
}
};
mediaRecorder.onstop = function() {
const blob = new Blob(recordedChunks, {
type: mimeType
});
recordedChunks = [];
showModal(function(filename) {
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = `${filename || 'recording'}.webm`;
downloadLink.click(); // Automatically trigger the download
});
};
mediaRecorder.start(200);
}
async function recordAudio() {
const mimeType = 'audio/webm';
shouldStop = false;
const stream = await navigator.mediaDevices.getUserMedia({audio: audioRecordConstraints});
handleRecord({stream, mimeType});
}
async function recordVideo() {
const mimeType = 'video/webm';
shouldStop = false;
const constraints = {
audio: {
'echoCancellation': true
},
video: {
'width': {
'min': 640,
'max': 1024
},
'height': {
'min': 480,
'max': 768
}
}
};
const stream = await navigator.mediaDevices.getUserMedia(constraints);
videoElement.srcObject = stream;
handleRecord({stream, mimeType});
}
async function recordScreen() {
const mimeType = 'video/webm';
shouldStop = false;
const constraints = {
video: {
cursor: 'motion'
}
};
if (!(navigator.mediaDevices && navigator.mediaDevices.getDisplayMedia)) {
return window.alert('Sorry, screen recording is not supported by this device.');
}
let stream = null;
const displayStream = await navigator.mediaDevices.getDisplayMedia({video: {cursor: "motion"}, audio: {'echoCancellation': true}});
if (window.confirm('Record screen and audio?')) {
const audioContext = new AudioContext();
const voiceStream = await navigator.mediaDevices.getUserMedia({ audio: {'echoCancellation': true}, video: false });
const userAudio = audioContext.createMediaStreamSource(voiceStream);
const audioDestination = audioContext.createMediaStreamDestination();
userAudio.connect(audioDestination);
if (displayStream.getAudioTracks().length > 0) {
const displayAudio = audioContext.createMediaStreamSource(displayStream);
displayAudio.connect(audioDestination);
}
const tracks = [...displayStream.getVideoTracks(), ...audioDestination.stream.getTracks()];
stream = new MediaStream(tracks);
handleRecord({stream, mimeType});
} else {
stream = displayStream;
handleRecord({stream, mimeType});
}
videoElement.srcObject = stream;
}