Created
April 23, 2018 16:34
-
-
Save raloliver/bb147b288a1472f94a61d91ae70afa46 to your computer and use it in GitHub Desktop.
frontal-camera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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