Skip to content

Instantly share code, notes, and snippets.

@rainyjune
Last active October 12, 2020 12:41
Show Gist options
  • Save rainyjune/6a3ed9de500f95b705cd578ceb37aeed to your computer and use it in GitHub Desktop.
Save rainyjune/6a3ed9de500f95b705cd578ceb37aeed to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>getUserMedia</title>
</head>
<body>
<video id="camera"></video>
<script>
document.addEventListener('DOMContentLoaded', onDOMContentLoaded, false);
function onDOMContentLoaded() {
console.log('DOMContentLoaded');
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
alert('Sorry, your browser does not support getUserMedia()');
return false;
}
var constraints = { audio: true, video: true };
navigator.mediaDevices.getUserMedia(constraints).then(function(mediaStream) {
debugger;
var video = document.querySelector('#camera');
video.srcObject = mediaStream;
video.onloadedmetadata = function() {
video.play();
};
/* use the stream */
}).catch(handleGetUserMediaError);
}
function handleGetUserMediaError(err){
/* handle the error */
switch (err.name) {
case 'PermissionDeniedError':
alert('Permission Denied');
break;
case 'AbortError':
alert('Aborted, some problem occurred which prevented the device from being used');
break;
case 'NotAllowedError':
alert('Operation not allowed');
break;
case 'NotFoundError':
alert('No media tracks of the type specified were found that satisfy the given constraints.');
break;
case 'NotReadableError':
alert('An error occurred which prevented access to the device');
break;
case 'OverConstrainedError':
alert('The specified constraints resulted in no candidate devices which met the criteria requested');
break;
case 'SecurityError':
alert('User media support is disabled');
break;
case 'TypeError':
alert('The list of constraints specified is empty, or has all constraints set to false.');
break;
default:
alert('Unknown Error');
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment