Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
track.stop is not turning the camera off anymore

I have a webpage where I want user to take a picture with his laptop/phone camera. Once he clicks on a button a modal is shown and the following js will start the camera stream to take the picture:

    function startStreaming() {
        if (null != cameraStream) {
            var track = cameraStream.getTracks()[0];
            track.stop();
            stream.load();
            cameraStream = null;
        }
        //const audioSource = audioInputSelect.value;
        const videoSource = videoSelect.value;
        const constraints = {
            //audio: {deviceId: audioSource ? {exact: audioSource} : undefined},
            video: {
                deviceId: videoSource ? {
                    exact: videoSource
                } : undefined
            }
        };
        navigator.mediaDevices.getUserMedia(constraints).then(gotStream).then(gotDevices).catch(handleError);
        var mediaSupport = 'mediaDevices' in navigator;
        if (mediaSupport && null == cameraStream) {
            const videoSource = videoSelect.value;
            const constraints = {
                video: {
                    deviceId: videoSource ? {
                        exact: videoSource
                    } : undefined
                }
            };
            navigator.mediaDevices.getUserMedia(constraints)
                .then(function (mediaStream) {
                    cameraStream = mediaStream;
                    stream.srcObject = mediaStream;
                    stream.play();
                })
                .catch(handleError);
        } else {
            alert('Your browser does not support media devices.');
            return;
        }
    }

This is triggered by

            $('#photoStudio').on('show.bs.modal', function (event) {
                navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);
                startStreaming();
            });

Then when I close the modal I want to stop the streaming but the led indicator next to my camera is still on)

            $('#photoStudio').on('hide.bs.modal', function (event) {
                stopStreaming();
            });

where stopStreaming() is:

    function stopStreaming() {
        if (null != cameraStream) {
            var track = cameraStream.getTracks()[0];
            track.stop();
            stream.load();
            cameraStream = null;
        }
    }

I don't get any kind of error and I cannot find a way to debug why the camera is still running. Am I missing anything in the stopStreaming function?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If any track has not been stopped then your camera will still be active. In your stopStreaming function you only stop the first track in the returned array.

If you instead iterate through the tracks you may catch ones you aren't currently:

    function stopStreaming() {
        if (null != cameraStream) {
            var tracks = cameraStream.getTracks();
           // stop all the tracks, not just the first
            tracks.forEach((track) => {
                track.stop();
           });
            stream.load();
            cameraStream = null;
        }
    }
about 4 years ago · Juan Pablo Isaza Report

0

this.camera_stream.getTracks().forEach((track) => {
    console.log(track);
    track.stop();
    **track.enabled = false**        
});
video.load()
this.camera_stream = null
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!