0

Edit: I've seen the answer but I do I embed it in my code? I've done several attempts but no one works. Do I leave my variables and add the ones from previos answer of should I replace them? Any example? Thanks.

first of all: javascript noob speaking, so forgive my mistakes please.

I implemented the code below in order to stop a video stream after (x) seconds have passed. What this code does correctly is to pause the video but I would also the code to release the webcam and the microphone which, in fact, remain still engaged after the video has been paused.

I've found other questions related to this topic (release each of audio and video tracks) but the point is that none of them were embedded in a script like this which, as a main condition, has a countdown to zero. I've tried to implement them but none of them actually worked or, maybe, I couldn't implement them properly.

Any clue?

Thanks

<script>
var startTime = new Date().getTime();
var interval = setInterval(function() {
    if (new Date().getTime() - startTime > 5000) {
        var video =
            document.getElementById("videoElement");
        video.src = "";
        video.pause();

    }

});
var video = document.getElementById("video"),
    vendorUrl = window.URL || window.webkitURL;
navigator.getMedia = navigator.getUserMedia ||
    navigator.webkitGetUserMedia ||
    navigator.mozGetUserMedia ||
    navigator.oGetUserMedia ||
    navigator.msGetUserMedia;

var video = document.querySelector("#videoElement");
if (navigator.mediaDevices.getUserMedia) {
    navigator.mediaDevices.getUserMedia({
            audio: true,
            video: true
        })
        .then(function(stream) {
            video.srcObject = stream;
        })

}
</script>
Frankie
  • 1
  • 1
  • 1
    You can find your answer [here](https://stackoverflow.com/q/11642926/2401386), you actually have to call `stream.getTracks().forEach(track => track.stop())` to stop the video for instance. – Blauharley Aug 02 '18 at 20:03
  • Thanks for the tip but I actually have already seen that solution but, embedded in my code it doesn't work or, maybe, I don't know how to insert that in my code properly. – Frankie Aug 02 '18 at 20:23

0 Answers0