Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery webcam/flash: How to detect if webcam is active?

I'm using this jQuery webcam plugin in a website I'm working on. If you go to the website, you'll notice it uses flash, and you have to click 'accept' in order to get it to work.

I want to determine if the webcam is active (on) or not. How can I do this with javascript/jquery?

In other words, what do I substitute in the if statement here?

function capture_image(){
    if(webcam.muted == true) {alert("hey");}

I'm far from a pro in javascript and jquery, so real code would be much appreciated.

Also, if someone could tell me how to capture the event of the webcam being activated, it would also be much appreciated. If you can't figure it out, don't worry about it.

like image 658
varatis Avatar asked Oct 08 '22 20:10

varatis


1 Answers

Add the following function to the debug option of the webcam.

$("#camera").webcam({
    width: 320,
    // other options etc.
    debug: function(type, message) { 
        if (message === "Camera started") { window.webcam.started = true; } 
    }
});

We cannot test for inactivity to be true (i.e. muted === true) but now we can test for webcam.started being true or undefined/false:

function capture_image(){
    if( !webcam.started ) { alert("hey, camera not started"); }
}

How to capture the event

There is not an event per se, other than the debug event. You could make one...

First do the following:

$("#camera").webcam({
    width: 320,
    // other options etc.
    debug: function(type, string) { 
        if (string === "Camera started") { 
            window.webcam.started = true; 
            if (window.webcam.onStarted) { window.webcam.onStarted(); } 
        } 
    }
});

Next add a function to our new event webcam.onStarted:

window.webcam.onStarted = function () {
    alert("Whey, the webcam started");
};
like image 160
Ross Avatar answered Oct 12 '22 10:10

Ross