hugh
hugh

Reputation: 171

Capturing video from web browser in HTML5

I'm trying to follow this guide on capturing video from webcam in HTML5

http://www.html5rocks.com/en/tutorials/getusermedia/intro/

I have copied and pasted the following code but Chrome does not ask for permission to use my camera

<video autoplay></video>

<script>
  var onFailSoHard = function(e) {
    console.log('Reeeejected!', e);
  };

  // Not showing vendor prefixes.
  navigator.getUserMedia({video: true, audio: true}, function(localMediaStream) {
    var video = document.querySelector('video');
    video.src = window.URL.createObjectURL(localMediaStream);

    // Note: onloadedmetadata doesn't fire in Chrome when using it with getUserMedia.
    // See crbug.com/110938.
    video.onloadedmetadata = function(e) {
      // Ready to go. Do some stuff.
    };
  }, onFailSoHard);
</script>

Whereas when I click "capture video" in the guide it works, my webcam shows...

Another website has similar code but yet again it's not working for me

http://dev.opera.com/articles/view/playing-with-html5-video-and-getusermedia-support/

<!-- HTML code -->
<video id="sourcevid" autoplay>Put your fallback message here.</video>

/* JavaScript code */
window.addEventListener('DOMContentLoaded', function() {
    // Assign the <video> element to a variable
    var video = document.getElementById('sourcevid');

    // Replace the source of the video element with the stream from the camera
    if (navigator.getUserMedia) {
        navigator.getUserMedia('video', successCallback, errorCallback);
        // Below is the latest syntax. Using the old syntax for the time being for backwards compatibility.
        // navigator.getUserMedia({video: true}, successCallback, errorCallback);
        function successCallback(stream) {
            video.src = stream;
        }
        function errorCallback(error) {
            console.error('An error occurred: [CODE ' + error.code + ']');
            return;
        }
    } else {
        console.log('Native web camera streaming (getUserMedia) is not supported in this browser.');
        return;
    }
}, false);

I was wondering if I'm missing something or has something changed, because none of the sample code has worked for me so far.

Upvotes: 1

Views: 1052

Answers (1)

hugh
hugh

Reputation: 171

Found out what was happening. For anyone wondering, on Chrome you only have access to the webcam if running from a server. It won't work on just a file.

Upvotes: 1

Related Questions