Search code examples
webrtcgetusermedia

getUserMedia Error webRTC


I'm starting with webRTC and am trying to access to my camera, however, the code doesn't work, although there is no mistakes in it.

The code is:

navigator.getUserMedia = ( navigator.getUserMedia ||           
                               navigator.webkitGetUserMedia || navigator.mozGetUserMedia 
                            || navigator.msGetUserMedia);
if (navigator.getUserMedia){
    var constrains ={video:true};
    function successCallback(localMediaStream){
        var video = document.querySelector("video");
        window.stream = localMediaStream;
        video.src = window.URL.createObjectURL(localMediaStream);
        video.onloadedmetadata =function(e){
            video.play();
        }
    }
    function errorCallback(error){
        console.log("Error: ",error);
    }
    navigator.getUserMedia(constrains,successCallback,errorCallback);
}else{
    alert('Sorry, the browser you are using doesn\'t support getUserMedia');
}

Can you help me please?


Solution

  • i am guessing that the code above is put in a html file and accessed directly by clicking on file( and url being like file:///...), this way would work in firefox, but not in chrome, for camera capture to work on chrome, you need to host the file in some server.

    also, on an unrelated note, you can replace

        video.onloadedmetadata =function(e){
            video.play();
        }
    

    with simply

        video.play();