How To Access Front and Rear Cameras With JavaScript's GetUserMedia DigitalOcean
How To Access Front and Rear Cameras With JavaScript's GetUserMedia DigitalOcean
1 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
Prerequisites
Conclusion
Chris Nwamba
2 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
With HTML5 came the introduction of APIs with access to device hardware, including
the MediaDevices API. This API provides access to media input devices like audio and
video.
With this API’s help, developers can access audio and video devices to stream and
display live video feeds in the browser. In this tutorial, you’ll access the video feed from
the user’s device and display it in the browser using the getUserMedia method.
The getUserMedia API makes use of the media input devices to produce a MediaStream.
This MediaStream contains the requested media types, whether audio or video. Using
the stream returned from the API, video feeds can be displayed on the browser, which is
useful for real-time communication on the browser.
When used along with the MediaStream Recording API, you can record and store media
data captured on the browser. This API only works on secure origins like the rest of the
newly introduced APIs, but it also works on localhost and file URLs.
• A basic knowledge of JavaScript. If you are new to JavaScript, try checking out the
How To Code in JavaScript series.
This tutorial will first explain concepts and demonstrate examples with Codepen. In the
final step, you will create a functioning video feed for the browser.
First, you will see how to check if the user’s browser supports the mediaDevices API.
This API exists within the navigator interface and contains the current state and identity
of the user agent. The check is performed with the following code that can be pasted
into Codepen:
3 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
Copy {
if ('mediaDevices' in navigator && 'getUserMedia' in navigator.mediaDevices)
console.log("Let's get this party started")
}
First, this checks if the mediaDevices API exists within the navigator and then checks if
the getUserMedia API is available within the mediaDevices . If this returns true , you can
get started.
After confirming the browser’s support for getUserMedia , you need to request
permission to make use of the media input devices on the user agent. Typically, after a
user grants permission, a Promise is returned which resolves to a media stream. This
Promise isn’t returned when the permission is denied by the user, which blocks access
to these devices.
This section will cover the general concept of contraints . The constraints object is a
MediaStreamConstraints object that specifies the types of media to request and the
requirements of each media type. You can specify requirements for the requested
stream using the constraints object, like the resolution of the stream to use ( front ,
back ).
You must specify either audio or video when making the request. A NotFoundError will
be returned if the requested media types can’t be found on the user’s browser.
If you intend to request a video stream of 1280 x 720 resolution, you can update the
4 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
{ Copy
video: {
width: 1280,
height: 720,
}
}
With this update, the browser will try to match the specified quality settings for the
stream. If the video device can’t deliver this resolution, the browser will return other
available resolutions.
To ensure that the browser returns a resolution not lower than the one provided you will
have to make use of the min property. Here is how you could update the constraints
object to include the min property:
{ Copy
video: {
width: {
min: 1280,
},
height: {
min: 720,
}
}
}
This will ensure that the stream resolution returned will be at least 1280 x 720 . If this
minimum requirement can’t be met, the promise will be rejected with an
OverconstrainedError .
In some cases you may be concerned about saving data and need the stream to not
exceed a set resolution. This can come in handy when the user is on a limited plan. To
enable this functionality, update the constraints object to contain a max field:
{ Copy
video: {
width: {
min: 1280,
max: 1920,
5 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
},
height: {
min: 720,
max: 1080
}
}
}
With these settings, the browser will ensure that the return stream doesn’t go below
1280 x 720 and doesn’t exceed 1920 x 1080 .
Other terms that can be used includes exact and ideal . The ideal setting is typically
used along with the min and max properties to find the best possible setting closest to
the ideal values provided.
{ Copy
video: {
width: {
min: 1280,
ideal: 1920,
max: 2560,
},
height: {
min: 720,
ideal: 1080,
max: 1440
}
}
}
To tell the browser to make use of the front or back (on mobile) camera on devices, you
can specify a facingMode property in the video object:
{ Copy
video: {
width: {
min: 1280,
ideal: 1920,
max: 2560,
},
height: {
min: 720,
6 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
ideal: 1080,
max: 1440
},
facingMode: 'user'
}
}
This setting will make use of the front-facing camera at all times in all devices. To make
use of the back camera on mobile devices, you can alter the facingMode property to
environment .
{ Copy
video: {
...
facingMode: {
exact: 'environment'
}
}
}
enumerateDevices
When the enumerateDevices method is called, it returns all of the available input media
devices available on the user’s PC.
With the method, you can provide the user options on which input media device to use
for streaming audio or video content. This method returns a Promise resolved to a
MediaDeviceInfo array containing information about each device.
An example of how to make a use of this method is shown in the snippet below:
A sample response for each of the devices would look like the following:
{ Copy
deviceId: "23e77f76e308d9b56cad920fe36883f30239491b8952ae36603c650fd5d8fbgj"
groupId: "e0be8445bd846722962662d91c9eb04ia624aa42c2ca7c8e876187d1db3a3875"
7 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
kind: "audiooutput",
label: "",
}
A label won’t be returned unless an available stream is available, or if the user has
granted device access permissions.
You have gone through the process of requesting and getting access to the media
devices, configured constraints to include required resolutions, and selected the camera
you will need to record video.
After going through all these steps, you’ll at least want to see if the stream is delivering
based on the configured settings. To ensure this, you will make use of the <video>
element to display the video stream on the browser.
Like mentioned earlier, the getUserMedia method returns a Promise that can be
resolved to a stream. The returned stream can be converted to an object URL using the
createObjectURL method. This URL will be set as a video source.
You will create a short demo where we let the user choose from their available list of
video devices. using the enumerateDevices method.
Create an index.html file and update the contents with the code below:
index.html
8 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
<title>Document</title>
</head>
<body>
<div class="display-cover">
<video autoplay></video>
<canvas class="d-none"></canvas>
<div class="video-options">
<select name="" id="" class="custom-select">
<option value="">Select camera</option>
</select>
</div>
<div class="controls">
<button class="btn btn-danger play" title="Play"><i data-feather="play-circle
<button class="btn btn-info pause d-none" title="Pause"><i data-feather
<button class="btn btn-outline-success screenshot d-none" title="ScreenShot
</div>
</div>
<script src="https://unpkg.com/feather-icons"></script>
<script src="script.js"></script>
</body>
</html>
In the snippet above, you have set up the elements you will need and a couple of
controls for the video. Also included is a button for taking screenshots of the current
video feed.
Create a style.css file and copy the following styles into it. Bootstrap was included to
reduce the amount of CSS you will need to write to get the components going.
style.css
.screenshot-image { Copy
width: 150px;
height: 90px;
border-radius: 4px;
border: 2px solid whitesmoke;
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.1);
position: absolute;
9 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
bottom: 5px;
left: 10px;
background: white;
}
.display-cover {
display: flex;
justify-content: center;
align-items: center;
width: 70%;
margin: 5% auto;
position: relative;
}
video {
width: 100%;
background: rgba(0, 0, 0, 0.2);
}
.video-options {
position: absolute;
left: 20px;
top: 30px;
}
.controls {
position: absolute;
right: 20px;
top: 20px;
display: flex;
}
10 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
.controls button {
margin: 5px 0 !important;
}
}
.controls button:nth-child(1) {
border: 2px solid #D2002E;
}
.controls button:nth-child(2) {
border: 2px solid #008496;
}
.controls button:nth-child(3) {
border: 2px solid #00B541;
}
11 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
The next step is to add functionality to the demo. Using the enumerateDevices method,
you will get the available video devices and set it as the options within the select
element. Create a file called script.js and update it with the following snippet:
script.js
feather.replace(); Copy
const constraints = {
video: {
width: {
min: 1280,
ideal: 1920,
max: 2560,
},
height: {
min: 720,
ideal: 1080,
max: 1440
},
}
};
12 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
play.onclick = () => {
if (streamStarted) {
video.play();
play.classList.add('d-none');
pause.classList.remove('d-none');
return;
}
if ('mediaDevices' in navigator && navigator.mediaDevices.getUserMedia) {
const updatedConstraints = {
...constraints,
deviceId: {
exact: cameraOptions.value
}
};
startStream(updatedConstraints);
}
};
getCameraSelection();
In the snippet above, there are a couple of things going on. Let’s break them down:
�� feather.replace() : this method call instantiates feather, which is an icon set for
web development.
�� The constraints variable holds the initial configuration for the stream. This will be
extended to include the media device the user chooses.
�� getCameraSelection : this function calls the enumerateDevices method. Then, you
filter through the array from the resolved Promise and select video input devices.
From the filtered results, you create <option> for the <select> element.
�� Calling the getUserMedia method happens within the onclick listener of the play
button. Here, you will check if this method is supported by the user’s browser
before starting the stream.
�� Next, you will call the startStream function that takes a constraints argument. It
13 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
Next, you will add click listeners to the button controls on the page to pause , stop , and
take screenshots . Also, you will add a listener to the <select> element to update the
stream constraints with the selected video device.
script.js
... Copy
cameraOptions.onchange = () => {
const updatedConstraints = {
...constraints,
deviceId: {
exact: cameraOptions.value
}
};
startStream(updatedConstraints);
};
pause.onclick = pauseStream;
screenshot.onclick = doScreenshot;
Now, when you open the index.html file in the browser, clicking the button will
start the stream.
14 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
https://codepen.io/chrisbeast/pen/ebYwpX
This tutorial introduced the getUserMedia API. It is an interesting addition to HTML5 that
eases the process of capturing media on the web.
The API takes a parameter ( constraints ) that can be used to configure the access to
audio and video input devices. It can also be used to specify the video resolution
required for your application.
You can extend the demo further to give the user an option to save the screenshots
taken, as well as recording and storing video and audio data with the help of
MediaStream Recording API.
Thanks for learning with the DigitalOcean Community. Check out our offerings
for compute, storage, networking, and managed databases.
15 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
Leave a comment���
You can type in this text area to quickly search our full set of tutorials,
documentation & marketplace offerings and insert the link!
16 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
• April 7, 2021
This code as-is gives a black screen. It seems the issue is that the “deviceId”
parameter is set outside of the “video” section in “updatedConstraints”. Putting
“deviceId” in the video section fixes the camera selection and the black screen
issue. “updatedConstraints” assignment needs to be re-written to include
“deviceId” inside the “video” section. It would be best to just convert
“constraints” to a variable and dynamically assign the “deviceId” as needed
instead of trying to compose another constant.
Reply
This doesn not work on ios 14. The camera’s are recognized, but I get a black
screen. Do you know why?
Reply
Hello, I can call the camera when I test locally, but I can’t find the media device
when I upload it to the server. Do you know why?
Reply
17 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
Click below to sign up and get to try our products over 60 days!
18 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
19 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
20 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
Get paid to write technical tutorials and select a tech-focused charity to receive
a matching donation.
21 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
Sign up and get $200 in credit for your first 60 days with DigitalOcean.
22 of 23 5/25/24, 02:58
How To Access Front and Rear Cameras with JavaScript... https://www.digitalocean.com/community/tutorials/fron...
23 of 23 5/25/24, 02:58