-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
51 lines (46 loc) · 1.7 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Webcams</title>
</head>
<body>
<select id="webcamSelect">
<option disabled selected>Select a webcam...</option>
</select>
<button id="startButton">Start Camera</button>
<video id="videoElement" autoplay></video>
<script>
const videoElement = document.getElementById('videoElement');
async function populateWebcams() {
const webcamSelect = document.getElementById('webcamSelect');
const devices = await navigator.mediaDevices.enumerateDevices();
devices.forEach(device => {
if (device.kind === 'videoinput') {
const option = document.createElement('option');
option.value = device.deviceId;
option.text = device.label || `Camera ${webcamSelect.options.length + 1}`;
webcamSelect.appendChild(option);
}
});
}
async function startCamera() {
const selectedDeviceId = document.getElementById('webcamSelect').value;
if (!selectedDeviceId) {
alert('Please select a webcam first.');
return;
}
const constraints = {
video: { deviceId: selectedDeviceId },
};
try {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
videoElement.srcObject = stream;
} catch (error) {
console.error('Error accessing webcam:', error);
}
}
populateWebcams();
document.getElementById('startButton').addEventListener('click', startCamera);
</script>
</body>
</html>