/    Sign up×
Community /Pin to ProfileBookmark

Help with ‘Reset’ button

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 yet. I’m trying to add a ‘reset’ 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.

`<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>–>`

And the js:

` 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();
});
stream = await navigator.mediaDevices.getUserMedia({
audio: true,
video: true,
});
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;
}
blob = new Blob(blobs, { type: mediaRecorder.mimeType });
video.srcObject = null;
video.src = URL.createObjectURL(
blob,
);
}`

to post a comment

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJul 01.2021 — Try this code for resetting:
``<i>
</i> function reset() {
stream.getTracks().forEach((track) =&gt; track.stop());
displaying = false;
if (recording) {
mediaRecorder.onstop = null;
mediaRecorder.stop();
recording = false;
}
}<i>
</i>
``
Copy linkTweet thisAlerts:
@chrisjchrisjauthorJul 01.2021 — Thanks again for your assistance.

I tried this without success, nothing happens when I select the 'reset' button:

&lt;button id="reset" onclick="reset()"&gt;Reset&lt;/button&gt;

let blobs = [];<br/>
let stream, mediaRecorder, blob;<br/>
let video = document.getElementById("video");<br/>
var displaying = false;<br/>
var recording = false;<br/>
async function display() {<br/>
stream = await navigator.mediaDevices.getUserMedia({<br/>
audio: true,<br/>
video: true,<br/>
});<br/>
video.srcObject = stream;<br/>
displaying = true;<br/>
}

<i> </i><CODE> function reset() {
<i> </i> stream.getTracks().forEach((track) =&gt; track.stop());
<i> </i> displaying = false;
<i> </i> if (recording) {
<i> </i> mediaRecorder.onstop = null;
<i> </i> mediaRecorder.stop();
<i> </i> recording = false;
<i> </i> }
<i> </i> }


<i> </i> async function startRecording() {
<i> </i> etc........
<i> </i>

any additional guidance is welcomed
Copy linkTweet thisAlerts:
@SempervivumJul 01.2021 — For me it works:
  • - Click camera view -> The camera view is displayed.

  • - Click reset -> The view turns black.

  • - Click camera view again -> The camera view is displayed again.


  • When recording is active, the result is the same: The view turns black and can be started again be clicking "camera view".

    How did you test it?
    Copy linkTweet thisAlerts:
    @chrisjchrisjauthorJul 01.2021 — Thanks again. I initiate camera view, then start recording, then stop recording, it plays back the recording automatically, and then I select Reset button, and nothing happens. Maybe I have function reset() in the wrong place?
    Copy linkTweet thisAlerts:
    @SempervivumJul 02.2021 — I see, we have to reset the src of the video in order to stop playback. Additionally the blobs have to be emptied:
    ``<i>
    </i> function reset() {
    stream.getTracks().forEach((track) =&gt; track.stop());
    displaying = false;
    if (recording) {
    mediaRecorder.onstop = null;
    mediaRecorder.stop();
    recording = false;
    }
    video.src = null;
    video.setAttribute('controls', false);
    blobs = [];
    }<i>
    </i>
    ``
    ×

    Success!

    Help @chrisjchrisj spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.14,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...