Skip to content

Instantly share code, notes, and snippets.

@thisgeek
Created September 6, 2012 01:21
Show Gist options
  • Save thisgeek/3649563 to your computer and use it in GitHub Desktop.
Save thisgeek/3649563 to your computer and use it in GitHub Desktop.
navigator.getUserMedia() demo
<!DOCTYPE HTML>
<html>
<head>
<title>HTML5 Webcam Test</title>
</head>
<body>
<h1><code>navigator.getUserMedia()</code></h1>
<video autoplay controls></video>
<p>
<button id="capture">Capture video</button>
<button id="stop">Stop</button>
</p>
<script src="webvideo.js"></script>
</body>
</html>
(function() {
var video, localMediaStream;
window.URL = window.URL || window.webkitURL;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
video = document.querySelector('video');
document.querySelector('button#capture').addEventListener('click', function () {
navigator.getUserMedia({audio: true, video: true}, function(stream) {
localMediaStream = stream;
video.src = window.URL.createObjectURL(stream);
}, function(e) {
console.log(e);
});
});
document.querySelector('button#stop').addEventListener('click', function(e) {
video.pause();
localMediaStream.stop();
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment