forked from tony-xlh/getUserMedia-multiple-camera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·106 lines (100 loc) · 3.32 KB
/
index.html
File metadata and controls
executable file
·106 lines (100 loc) · 3.32 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Multiple Camera Simple Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<style>
</style>
</head>
<body>
<div>
<label>
Camera 1:
<select id="select-camera-1"></select>
</label>
<label>
Camera 2:
<select id="select-camera-2"></select>
</label>
</div>
<div>
<video id="camera1" controls autoplay playsinline></video>
</div>
<div>
<video id="camera2" controls autoplay playsinline></video>
</div>
<button id="btn-open-camera">Open Cameras</button>
<script>
var devices;
var camSelect1 = document.getElementById("select-camera-1");
var camSelect2 = document.getElementById("select-camera-2");
window.onload = async function(){
await listDevices()
}
async function listDevices(){
devices = await getCameraDevices()
for (let index = 0; index < devices.length; index++) {
const device = devices[index];
camSelect1.appendChild(new Option(device.label ?? "Camera "+index,device.deviceId));
camSelect2.appendChild(new Option(device.label ?? "Camera "+index,device.deviceId));
}
camSelect2.selectedIndex = 1;
}
async function getCameraDevices(){
await askForPermissions();
var allDevices = await navigator.mediaDevices.enumerateDevices();
var cameraDevices = [];
for (var i=0;i<allDevices.length;i++){
var device = allDevices[i];
if (device.kind == 'videoinput'){
cameraDevices.push(device);
}
}
return cameraDevices;
}
async function askForPermissions(){
var stream;
try {
var constraints = {video: true, audio: false}; //ask for camera permission
stream = await navigator.mediaDevices.getUserMedia(constraints);
} catch (error) {
console.log(error);
}
closeStream(stream);
}
function closeStream(stream){
try{
if (stream){
stream.getTracks().forEach(track => track.stop());
}
} catch (e){
alert(e.message);
}
}
document.getElementById("btn-open-camera").addEventListener("click",function(){
captureCamera(document.getElementById("camera1"),camSelect1.selectedOptions[0].value);
captureCamera(document.getElementById("camera2"),camSelect2.selectedOptions[0].value);
});
function captureCamera(video, selectedCamera) {
var constraints = {
audio:false,
video:true
}
if (selectedCamera) {
constraints = {
video: {deviceId: selectedCamera},
audio: false
}
}
navigator.mediaDevices.getUserMedia(constraints).then(function(camera) {
video.srcObject = camera;
}).catch(function(error) {
alert('Unable to capture your camera. Please check console logs.');
console.error(error);
});
}
</script>
</body>
</html>