how ( stop,exit ) video in webrtc navigator user media JavaScript

Fernando josé picture Fernando josé · Oct 23, 2016 · Viewed 7.2k times · Source

how i stop and exit in pure js, stream webcam in WEBRTC api js , i have in my code the following script :

<script type="text/javascript">
$(document).ready(function() {
    $("#abrirModal").click(function() {
        navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
        var constraints = {
            audio: false,
            video: true
        };
        var live = document.getElementById("live");

        function successCallback(stream) {
            window.stream = stream; // stream available to console
            if (window.URL) {
                live.src = window.URL.createObjectURL(stream);
            } else {
                live.src = stream;
            }
            $("#myModal").modal("show");
            window.setTimeout(function() {
                $("#myModal").modal("hide");
            }, 9000);
        }

        function errorCallback(error) {
            console.log("navigator.getUserMedia error: ", error);
        }

     navigator.getUserMedia(constraints, successCallback, errorCallback);
    });
});
 </script>

how close and exit web cam in other file.js for example:

  function exitWebCam () {  
     document.getElementById("live")."close the web cam";
  }

Answer

Philipp Hancke picture Philipp Hancke · Oct 23, 2016

You end a stream by closing all of its tracks: stream.getTracks().forEach(function(track) { track.stop(); })