-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (65 loc) · 2.08 KB
/
index.js
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
var stream;
var videoContainer = document.querySelector('#webcam');
var videoSelect = document.querySelector('#cameras');
var botoes = {
iniciar: document.querySelector('#iniciar'),
parar: document.querySelector('#parar'),
};
// Ao carregar o DOM, as funções serão inicializadas
document.addEventListener('DOMContentLoaded', (ev) => carregarCameras());
videoSelect.onchange = (ev) => initCamera(ev.value);
// videoSelect.addEventListener('change', (ev) => initCamera(ev.value)); // Dá pra fazer dessa forma também
async function carregarCameras() {
alternarExibicaoVideo(false);
const devices = await navigator.mediaDevices.enumerateDevices();
for (const d of devices) {
if (d.kind === 'videoinput') {
setNomeCamera(d);
}
}
// initCamera(); // comentado para não iniciar as câmeras logo de inicio
}
async function setNomeCamera(camera) {
const option = document.createElement('option');
option.value = camera.deviceId;
option.text = camera.label;
if (!camera.label) {
await solicitarPermissao();
await carregarCameras();
return;
}
videoSelect.appendChild(option);
}
async function solicitarPermissao() {
const mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: true });
mediaStream.getTracks().forEach(t => t.stop());
}
async function initCamera(idCamera = videoSelect.value) {
pararCamera();
const constrains = {
video: {
deviceId: idCamera ? { exact: idCamera } : undefined
}
};
stream = await navigator.mediaDevices.getUserMedia(constrains);
videoContainer.srcObject = stream;
alternarExibicaoVideo(true);
}
function pararCamera() {
stream?.getTracks().forEach(t => {
// console.log('Parando o video', t, t.getCapabilities());
t.stop();
});
alternarExibicaoVideo(false);
}
function alternarExibicaoVideo(exibir) {
if (exibir) {
videoContainer.removeAttribute('hidden');
botoes.iniciar.disabled = true;
botoes.parar.disabled = false;
} else {
videoContainer.setAttribute('hidden', true);
botoes.iniciar.disabled = false;
botoes.parar.disabled = true;
}
}