Toggle camera on and off using a single button in Javascript

I'm encountering an issue with my camera functionality. I've implemented a button to open the camera (see code below), but I'm unsure how to turn off the camera when the same button is clicked.

Snippet of my code:

Button:

<a type="button" onclick="cameraOff()" name="button" id="button"> <i class="fas fa-video fa-2x cam-btn"></i> </a> 

<video id="video" width="640" height="480" autoplay></video>

<script>
    function cameraOff() {

        var video = document.getElementById('video');
        if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
            navigator.mediaDevices.getUserMedia({
                video: true
            }).then(function(stream) {
                video.srcObject = stream;
                video.play();
            });
        }
    }
</script>

Answer №2

substitute the code snippet

video.srcObject = stream; video.play();
for
video.srcObject={}; video.pause(); stream.stop();
link to my solution

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving data from a nested array within an array of objects using JavaScript

Below is an example of an array object: const data = [ { name: "A", values: [ { name: "PASS", value: 36, }, ], }, { name: "B", values: [ { ...

What is the best way to expand the footer specifically for mobile devices?

My website has a footer positioned at the bottom of the page. The HTML and CSS for this footer are as follows: <style> html { position: relative; min-height: 100%; } body { /* Margin bottom by footer height */ ...

Iterate through a JavaScript object while preserving the key along with its corresponding value

var info = { 2016-09-24: { 0: {amount: 200, id: 2}, 1: {...} }, 2016-09-25: { 0: {amount: 500, id: 8}, 1: {...} } } In order to display the data stored in the object above, I want to create a view that looks like this: "**" will r ...

Using AngularJS to send data from a controller to a factory

Looking to implement a basic pagination feature with data from the backend. Each page should display 10 activities. /feed/1 -> displays 0-10 /feed/2 -> displays 10-20 /feed/3 -> displays 20-30 The goal is to start at page 1 and increment the pag ...

Button for searching through the Bootstrap navigation bar

I'm currently working on adding a search menu to the navbar in two different designs - one for screens below 767px and another for screens above 767px. Although I have been successful in expanding the search bar, I am facing issues with the correct p ...

The list item click event is not triggered when new list items are added

I've run into a bit of confusion with my code. Everything seems to be working perfectly fine until I introduce new items. Take a look at a sample of my items However, once I make changes to my list, the click function stops working. Check out a sa ...

Logs from console.log() statements in Firebase functions are missing

I've encountered an issue with the function below where the console.log() statements do not appear in the Firebase logs. When I remove everything after the db.collection call, the initial console.log() statements are visible. However, once I reintrodu ...

What is the best way to navigate to a specific location on a web page?

After clicking the "Add comment" link, a comment form popped up using Ajax. I now need assistance with scrolling to it, can you please help me out? ...

Retrieving data from a textbox using JavaScript within an Excel VBA setting

I'm encountering an issue with a Macro in Excel where I need to retrieve the value of an "input type="text"" on a JavaScript webpage. However, the value is not specified in the code (value=""), and it remains unchanged even when the webpage displays t ...

What is the correct way to implement Axios interceptor in TypeScript?

I have implemented an axios interceptor: instance.interceptors.response.use(async (response) => { return response.data; }, (err) => { return Promise.reject(err); }); This interceptor retrieves the data property from the response. The re ...

efficiently manage various nested levels of request parameters

I am configuring routes for my express app and need the following paths: /regions /regions/europe /regions/europe/france /regions/europe/france/paris Currently, I have set up individual route handlers for each path. Is there a more efficient way to ha ...

Decoding Lodash: Unveiling the findwhere and copy operator in the realm of

Could someone please explain why using Lodash to fetch an object with findWhere allows for a reference copy that enables dynamic changes based on user actions, while the same operation using the copy operator fails to update the source object? I have creat ...

A handy feature of HTML5 is the dataset attribute, which

Is there a way to restrict the height of the dataset dropdown list in HTML? When using <input list="datalist"> with numerous elements, some might not be visible. Is it possible to limit the size of the list and enable vertical scrolling to display al ...

Incorporating website data into my JavaScript code

I lack experience in extracting data from websites, but I'm interested in learning how to utilize data from: . I believe this data is in the form of an array and I want to loop through it to find an item with the name "example" and gather additional i ...

Discovering the trigger for a CGI script execution

My goal is to enhance the functionality of my CGI script by implementing different actions based on the trigger that initiated the script. For instance, if a particular button is clicked, I want to clear a database. If another button is pressed, I aim to ...

Server not recognizing nested routes in React-Router

I am currently using Express.js to render a React application on the server side, but I have encountered an issue where some of my routes are getting skipped over unexpectedly. Here is my server.js: app.get('*', (req, res) => { conso ...

Leveraging jQuery in Content Scripts for Chrome Extensions

I am currently working on developing a Chrome extension that will prompt a small input whenever a user highlights text on a webpage (similar to Medium's feature that allows you to tweet highlighted text). While I am making progress, I believe using j ...

Unable to send a post request using ajax

Section of Form in home.php <div id='googleForm'> <form> <div class='item'> <label class='label'>Full Name</label> <input class=&apos ...

Appwrite error message: Unable to access properties of undefined (looking for 'endpoint')

I am currently working on a project using Appwrite and I have encountered an issue with SDKs. When I use a client-side SDK to interact with the functions of Appwrite Teams, everything works perfectly like this: import { Client, Teams } from "appwrite& ...

Display dynamic web content developed with JavaScript for public viewing

As a newcomer to the world of web development, I'm not sure if it's possible to achieve what I have in mind without using PHP. However, I'm willing to give it a shot. Currently, my webpage functions perfectly with JavaScript, HTML, and CSS, ...