1

I am executing this code on localhost XAMPP Server

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<video onclick="changeFilter(this);"width=200 height=200 id="video" controls autoplay></video> 
<p>
<button onclick="startWebcam();">Start WebCam</button>
<button onclick="stopWebcam();">Stop WebCam</button> 
</p>
 <script>
 navigator.getUserMedia = ( navigator.getUserMedia ||
                   navigator.webkitGetUserMedia ||
                   navigator.mozGetUserMedia ||
                   navigator.msGetUserMedia);

 var webcamStream;

 function startWebcam() {
 if (navigator.getUserMedia) {
 console.log("toto");
 navigator.getUserMedia (

    // constraints
    {
      video: true,
      audio: false
    },

    // successCallback
     function(localMediaStream) {
      var video = document.querySelector('video');
      video.src = window.URL.createObjectURL(localMediaStream);
      webcamStream = localMediaStream;
    },

    // errorCallback
       function(err) {
       console.log("The following error occured: " + err);
       }
     );
    } else {
    console.log("getUserMedia not supported");
      }  
     }

  function stopWebcam() {
  localMediaStream.stop();
  }
</script>
</body>
</html>

This code starts my webcam, but when I press the Stop WebCam button the console gives me the following error:

Uncaught TypeError: Cannot read property 'stop' of undefined function stopWebcam() { webcamStream.stop(); }

I am a JavaScript newbie and I can not see the issue here.

Madness
  • 2,730
  • 3
  • 20
  • 29
ciruela_g
  • 11
  • 3

1 Answers1

1

localMediaStream is not available to you in stopWebcam(). Check out this post for more on What is the scope of variables in JavaScript?

Try...

  function stopWebcam() {
      webcamStream.stop();
  }
Community
  • 1
  • 1
Madness
  • 2,730
  • 3
  • 20
  • 29
  • 1
    thank you so much Madness!!! lol i just realized i needed to change it to that and then it worked!! good luck to you and thank you for your help!!! – ciruela_g Aug 04 '15 at 01:42