Fernando josé Fernando josé - 1 month ago 21
Javascript Question

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";
}

Answer

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

Comments