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";
}
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.
Use stream. getTracks(). forEach(track => track. stop()); .
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.
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.
You end a stream by closing all of its tracks:
stream.getTracks().forEach(function(track) { track.stop(); })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With