Skip to content

Instantly share code, notes, and snippets.

@hyjk2000
Created October 22, 2015 02:54
Show Gist options
  • Select an option

  • Save hyjk2000/10e0bffa19181fc3f600 to your computer and use it in GitHub Desktop.

Select an option

Save hyjk2000/10e0bffa19181fc3f600 to your computer and use it in GitHub Desktop.
navigator.getUserMedia
<!DOCTYPE html>
<html>
<head>
<title>navigator.getUserMedia</title>
<style>
video.local { transform: rotateY(180deg); }
</style>
</head>
<body>
<video class="local" autoplay></video>
<script>
(function() {
var localVideo = document.querySelector("video.local");
var localMediaStream = null;
var localMediaURL = null;
function getMediaConstraints() {
if (navigator.webkitGetUserMedia) {
// Chrome
return {
video: {
mandatory: { minWidth: 640, minHeight: 480 },
optional: [{ minWidth: 1280 }, { minHeight: 720 }, { facingMode: "user" }]
},
audio: true
};
}
// Firefox
return {
video: {
width: { min: 640, ideal: 1280 },
height: { min: 480, ideal: 720 },
facingMode: "user"
},
audio: true
};
}
function getUserMedia(callback) {
if (navigator.getUserMedia === undefined) {
navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
if (navigator.getUserMedia === undefined) {
console.error('getUserMedia not supported.')
return false;
}
}
navigator.getUserMedia(getMediaConstraints(), callback, function(e) {
console.error('getUserMedia failed: ' + e.message);
});
}
getUserMedia(function(stream) {
localMediaStream = stream;
localVideo.src = localMediaURL = URL.createObjectURL(localMediaStream);
});
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment