Skip to content

Instantly share code, notes, and snippets.

@raloliver
Created April 23, 2018 16:34
Show Gist options
  • Save raloliver/bb147b288a1472f94a61d91ae70afa46 to your computer and use it in GitHub Desktop.
Save raloliver/bb147b288a1472f94a61d91ae70afa46 to your computer and use it in GitHub Desktop.
frontal-camera
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Choose a Camera</title>
</head>
<body>
<select name="" id="videoSelect"></select><br>
<video src="" controls autoplay></video>
<script src="script.js"></script>
</body>
</html>
navigator.mediaDevices.enumerateDevices().then((devices) => {
let options = '<option id="">Selecione uma Webcam</option>'
devices.forEach((device) => {
if (device.kind === 'videoinput') {
options += '<option id="' + device.deviceId + '">' + device.label + '</option>'
}
});
document.querySelector('#videoSelect').innerHTML = options;
})
document.querySelector('#videoSelect').addEventListener('change', function (e) {
startVideo(e.target.value);
})
startVideo = (id) => {
let config = {
video: {deviceId: id},
audio: false
}
let element = document.querySelector('video');
let success = (stream) => {
element.src = window.URL.createObjectURL(stream);
}
navigator.getUserMedia(config, success, (err) => console.log(err));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment