Search code examples
javascriptgoogle-chrome-extensiongetusermedia

Turn off webcam/camera after using getUserMedia


I'm developing a Chrome Extension that uses the background page to access the user's webcam.

Users are given the option to turn the camera off.

The stream appears to be being turned off. The relevant functions are no longer receiving the stream. However the webcam light (currently being developed and tested on a mac book pro) does not turn off.

Any ideas?

Here's my code for setting up the stream:

if (navigator.webkitGetUserMedia!=null) {

    var options = { 
        video:true, 
        audio:false 
    };  
    navigator.webkitGetUserMedia(options, 
        function(stream) { 
        vid.src = window.webkitURL.createObjectURL(stream);
        localstream = stream;
        vid.play();
        console.log("streaming");
    }, 
        function(e) { 
        console.log("background error : " + e);
        }); 
} 

And here's my method for turning off the stream:

function vidOff() {
    clearInterval(theDrawLoop);
    ExtensionData.vidStatus = 'off';
    vid.pause();
    vid.src = "";
    localstream.stop();
    DB_save();
    console.log("Vid off");
}

Any obvious I'm missing?


Solution

  • The code above works - as shown by @jib here using the above code:

    function vidOff() {
        vid.pause();
        vid.src = "";
        localstream.stop();
    }
    

    The problem is to do with it being a persistent background page. I'm swapping over to event pages for the Chrome extension as a work around.