0

I've written a script that successfully activates the user's webcam but throws an error when I try to stop or pause the stream.

I've built a prototype for multi-part single page webapp that requires the user to activate then deactivate their webcam. this version of the prototype doesn't actually capture and store webcam images and it's especially important that the user is able to deactivate the camera so that they don't feel like they're under surveillance.

Below are is my script, which uses a webRTC feature detection lib called compatibility. the line $video.pause() causes the following error

"Uncaught TypeError: undefined is not a function" in Chrome 36.x

while typing "$video" in devtools nad hitting return gives me this

[​​] but doing $video[0].pause() gives me the same error message as above.

I was thinking that doing something like window.URL.revokeObjectURL(stream) -- in my case compatibility.URL.revokeObjectURL(stream) -- was the way to go but wanted to get some advise before diving into that

// get it
var smoother = new Smoother(0.85, [0, 0, 0, 0, 0]),
    $video = $("#v"),
    $local_media = null;

webcam_capture = function(x){
    try {
        compatibility.getUserMedia({video: true}, function(stream) {
            try {
                $video.attr('src', compatibility.URL.createObjectURL(stream));          
            } catch (error) {
                $video.attr(src, stream);                       
            }   
            $('.camera_prompt').removeClass('show')
        }, function (error) {
            alert("WebRTC not available");
        });
    } catch (error) {
        console.log(error);
    }
}


webcam_capture();

// warn - prompt permissions after 6 seconds
capture_error = setTimeout(function(){
    typeof $video.attr('src') == "undefined" ?  
    $('.camera_prompt').addClass('show') :
    clearTimeout(capture_error);
},3000) 

start_card_capture = function(x){
    // open capture sesh
    OAO.card_capture_status = true;
    // get id of input to focus and class
    var id = 'capture_' + x;
    $('input#' + id).addClass('focused');
    // UI fade in
    $('#v, .notification, button.capture').fadeIn();
}

end_card_capture = function(x){
    // close capture sesh
    OAO.card_capture_status = false;    
    // UI fade out 
    $('#v, .notification, button.capture').fadeOut('visible');
}


capture_notify = function(x){
    $('#card_capture_form').find('.notification').find('div').text(x)
}


$('.capture').on('click', function(e){
    // determine input
    var $f = $(this).parent('form')
        $i = $f.find('input.focused');
    // check input and update the associated label's icon
    $i.prop('checked', true).removeClass('focused');
    $i.next('label').addClass('captured');
    // close UI

    if($f.find('label.captured').length < 2){
        end_card_capture(e)
    }

    if($f.find('label.captured').length == 2){
        $video.pause();
        OAO.sectional_transition('#funding',{y: '0%'}, 1400,100, $(this).parents('section'), { y: '-250%' }, 1000, 100  );
        OAO.step = 2;
        OAO.progress_bar(OAO.step)
        $('#progress_bar_container').removeClass('hidden');
    }

});


$('.capture_front, .capture_back').on('click', function(){
    // determine which side
    $(this).hasClass('capture_front') ?
    OAO.card_capture_side = 'front' :
    $(this).hasClass('capture_back') ?
    OAO.card_capture_side = 'back' :
    C('whichsideofthecardamIshooting?');


    if(typeof $video.attr('src') !== "undefined"){
        // set prop and init capture
        capture_notify(OAO.card_capture_side);
        start_card_capture(OAO.card_capture_side)
    }
  });

Thanks in advance for your help

fitsum
  • 1
  • 6
  • 1
    if `$("#v")` returns `[]`, then jquery couldn't find an element with that id – Andrew Vermie Aug 04 '14 at 15:56
  • Also, duplicate of [this question](http://stackoverflow.com/questions/15925010/stop-the-webcam-streaming-of-getusermedia-without-page-refreshing) – Andrew Vermie Aug 04 '14 at 15:58
  • Andrew could you point me to the duplicate? Also, shouldn't $('#v')[0].pause() work for Chrome in this case? – fitsum Aug 04 '14 at 16:54
  • I put a link in my comment already. Pausing the video element may not make _all_ users feel like their privacy is safe; there is still a part of the browser UI that indicates your page has access to the mic or camera (e.g. Chrome puts a red circle on the page's browser tab). – Andrew Vermie Aug 04 '14 at 17:07
  • I see the link now,and I also solved my problem. I set localMedia = stream then localMedia.stop() to stop the stream -- which if it wasn't clear, was my goal. I gathered from the linked question mine duplicated and other reference I used for guidance that doing stream.stop() didn't work on webkit so I didn't bother with it initially. then I added it as a fallback after $('video').pause() but it must have been misplaced becaused I got an undefined function error as well. – fitsum Aug 04 '14 at 17:33
  • MODS PLEASE MARK THIS 'SOLVED' THANKS! – fitsum Aug 04 '14 at 17:34

0 Answers0