kunz
kunz

Reputation: 1047

javascript webcam not working in browser

i have a webcam script but it doesnt seem to work i also dont get any console errors can some one please help me to see what m i doing wrong

Photo.js

    (function(){
    var video = document.getElementById('video'),
                    vendorURL = window.URL || window.webkitURL;

 navigator.getMedia = navigator.getUserMedia ||
                                                                                                                                  navigator.WebKitGetUserMedia||
                                                                                                                    navigator.msGetUserMedia;

                        navigator.getMedia({
                            video:true,
                            audio:false
                        }, function(stream) {
                            video.src = vendorURL.createObjectURL(stream);
                            video.play();
                        }, function(error) {
                            //an error occured
                            //error.code
                        });

    });

index.html

<div class="booth">
     <video id="video" width="400" height="300"></video>
   </div><!-- end booth -->

and here is a fiddle Fiddle

This tutorial is script is taken from https://youtu.be/gA_HJMd7uvQ?t=456

Upvotes: 1

Views: 2308

Answers (2)

guest271314
guest271314

Reputation: 1

There are two issues at javascript at Question. 1) The IIFE is not actually called; you can call the immediately invoked function expression by including opening and closing parentheses () before closing parenthesis ) 2) w and k at navigator.WebKitGetUserMedia should be lowercase characters navigator.webkitGetUserMedia

(function() {
  var video = document.getElementById('video'),
    vendorURL = window.URL || window.webkitURL;

  navigator.getMedia = navigator.getUserMedia ||
    navigator.webkitGetUserMedia || /* substituted `w` for `W`, `k` for `K` */
    navigator.msGetUserMedia;

  navigator.getMedia({
    video: true,
    audio: false
  }, function(stream) {
    video.src = vendorURL.createObjectURL(stream);
    video.play();
  }, function(error) {
    console.log(error)
    //an error occured
    //error.code
  });   
}()); // added `()` before closing `)`

jsfiddle https://fiddle.jshell.net/heLs62sg/1/

Upvotes: 2

Ben Broadley
Ben Broadley

Reputation: 632

It looks like you have wrapped this code in a closure which wont run unless you specifically call it from somewhere.

(function(){
    // Code will not execute automatically
});

Note the extra parenthesis on the final line in the following example:

(function(){
    // Code will execute automatically
})();

Alternatively, you could assign your code to a variable and manually call it, eg:

var func = (function(){
    // Some Code in here
});

func();

I suspect that this is the root cause of your problem, but I cant say for sure that your webcam code works but this should be a step in the right direction.

Upvotes: 0

Related Questions