Your turn off the camera and microphone by stopping the tracks you received from getUserMedia
:
stream.getTracks().forEach(track => track.stop())
To turn them back on, call getUserMedia
again.
You can build an in-content camera and/or microphone chooser using enumerateDevices
which returns a list of, among other things, deviceId
s which you can use with the deviceId
constraint to pick a specific device with getUserMedia
(use https fiddle in Chrome):
var log = msg => div.innerHTML += msg + "<br>";
navigator.mediaDevices.enumerateDevices()
.then(devices => {
var cams = devices.filter(device => device.kind == "videoinput");
log("You have " + cams.length + " camera(s).");
if (cams.length) {
return navigator.mediaDevices.getUserMedia({
video: { deviceId: { exact: cams[0].deviceId } },
})
.then(stream => (video.srcObject = stream));
}
})
.catch(log);
<div id="div"></div><br>
<video id="video" height="120" width="160" autoplay></video>
Note that adapter.js is needed to work with Chrome 52 (fixed in 54).
See the official device selector sample for an already complete demo of that.