-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
31 lines (27 loc) · 1001 Bytes
/
script.js
File metadata and controls
31 lines (27 loc) · 1001 Bytes
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
const videoElement = document.getElementById('videoElement');
const startButton = document.getElementById('startButton');
const stopButton = document.getElementById('stopButton');
let stream;
async function startStream() {
try {
stream = await navigator.mediaDevices.getUserMedia({ video: true });
videoElement.srcObject = stream;
startButton.disabled = true;
stopButton.disabled = false;
} catch (err) {
console.error("Error accessing the camera:", err);
alert("Could not access the camera. Please check your permissions.");
}
}
function stopStream() {
if (stream) {
const tracks = stream.getTracks();
tracks.forEach(track => track.stop());
videoElement.srcObject = null;
startButton.disabled = false;
stopButton.disabled = true;
}
}
startButton.addEventListener('click', startStream);
stopButton.addEventListener('click', stopStream);
window.addEventListener('beforeunload', stopStream);