Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Help with 'Reset' button

chrisj

Bronze Coder
The code (below) allows the page vistor to click a ‘display’ button to show the camera view on the page, but not start the video recording. I'm trying to add a 'reset' the camera button, but I think it needs to stop the stream to do that. Any help with assisting me with the 'reset' button functionality is appreciated.

HTML:
 <video id="video" autoplay muted playsinline></video>
    <button id="display" onclick="display()">Camera View</button>
    <button id="start" onclick="startRecording()">Start Recording</button>
    <button id="stop" onclick="endRecording()">Stop Recording</button>

<!--<button id="reset" onclick="resetCamera()">Reset </button>-->

JavaScript:
    <script>
        let blobs = [];
        let stream, mediaRecorder, blob;
        let video = document.getElementById("video");
        var displaying = false;
        var recording = false;
        async function display() {
            stream = await navigator.mediaDevices.getUserMedia({
                audio: true,
                video: true,
            });
            video.srcObject = stream;
            displaying = true;
        }
        async function startRecording() {
            if (displaying) {
                // Stop all tracks of current stream
                stream.getTracks().forEach((track) => {
                    track.stop();
                });
                // Create new stream in order to start the timestamp from 0:
                stream = await navigator.mediaDevices.getUserMedia({
                    audio: true,
                    video: true,
                });
                // Switch on controls:
                video.setAttribute('controls', true);
                video.srcObject = stream;
                mediaRecorder = new MediaRecorder(stream);
                mediaRecorder.ondataavailable = (event) => {
                    if (event.data) {
                        blobs.push(event.data);
                    }
                };
                mediaRecorder.onstop = doPreview;
                // Let's receive 1 second blobs
                mediaRecorder.start(1000);
                recording = true;
            }
        }
        function endRecording() {
            if (recording) {
                mediaRecorder.stop();
                stream.getTracks().forEach((track) => track.stop());
                recording = false;
            }
        }
        function doPreview() {
            if (!blobs.length) {
                return;
            }
            // Let's concatenate blobs to preview the recorded content
            blob = new Blob(blobs, { type: mediaRecorder.mimeType });
            video.srcObject = null;
            video.src = URL.createObjectURL(
                blob,
            );
        }
    </script>
 
Your best bet is to make sure you use a function to generate or initiate a canvas element so that way you can destroy it and recreate, or clear the canvas and then repaint / render the camera when needed again.
 

Buy us a coffee!

Back
Top Bottom