Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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";
  }
like image 317
Fernando josé Avatar asked Oct 23 '16 12:10

Fernando josé


People also ask

How do I stop a stream in Javascript?

Stopping a video stream This works by obtaining the video element's stream from its srcObject property. Then the stream's track list is obtained by calling its getTracks() method. From there, all that remains to do is to iterate over the track list using forEach() and calling each track's stop() method.

How do I stop WebRTC streaming?

Use stream. getTracks(). forEach(track => track. stop()); .

Is getUserMedia deprecated?

getUserMedia() Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes.

How do I use Navigator MediaDevices getUserMedia?

When getUserMedia() is invoked, the browser asks for permission from the user to use the media inputs (camera or microphone or both) connected to the user device. Syntax : navigator. mediaDevices.


1 Answers

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

like image 190
Philipp Hancke Avatar answered Nov 14 '22 21:11

Philipp Hancke