Browser Super Powers: getUserMedia

In case you didn’t already believe it, your Web Browser has super powers. No longer is it something to merely display a document marked up with hypertext. No longer is it limited to the read-only text and images of the olden days (aka the last two decades or so). Oh no. Now that the browser […]


This content originally appeared on Robin Osborne and was authored by Robin Osborne

In case you didn’t already believe it, your Web Browser has super powers. No longer is it something to merely display a document marked up with hypertext.

No longer is it limited to the read-only text and images of the olden days (aka the last two decades or so). Oh no. Now that the browser wars have cooled down, and the commons group are collaborating and updating the W3C standards rapidly enough for the eager-beaver browser vendors, we’re seeing new functionality quickly adopted across all major browser.

One of these super powers allows us to access the user’s microphone and camera (with their permission) using a one-liner:

var promise = navigator.mediaDevices.getUserMedia(constraints);

where constraints define the device preferences, such as:

{
  audio: true,
  video: {
    width: { min: 1024, ideal: 1280, max: 1920 },
    height: { min: 776, ideal: 720, max: 1080 },
    facingMode: "user"
  }
}

Here we’re requesting permission to access the device’s microphone and the camera, with a minimum and maximum requirement around the camera resolution, as well as defining a preference for the front-facing camera if available (facingMode).

This is all just plain old javascript too! At the time of writing, you only need to worry about ye olde IE and Opera Mini not supporting it.

Don’t believe me? Go to a website that uses HTTPS, open your browser’s Developer Tools and paste this in to the console:

var constraints = { audio: true, video: true }; 

navigator.mediaDevices.getUserMedia(constraints)
.then(function(mediaStream) {
    var video = document.createElement('video');
    document.getElementsByTagName('body')[0].appendChild(video);
    video = document.querySelector('video');
    video.srcObject = mediaStream;
    video.onloadedmetadata = function(e) {
        video.play();
    };
})
.catch(function(err) { console.log(err.name + ": " + err.message); });

You’ll be prompted for permission to access your devices:
browser requesting permission to access camera

If you allow, then you can scroll to the bottom of the page and see your lovely face appear in a dynamically generated video tag:

dynamically added video element with my pretty face in it
Amazing, right?

Web browsers are getting closer to native apps in what they can achieve, and getUserMedia (aka Stream API) is just one example of this.


This content originally appeared on Robin Osborne and was authored by Robin Osborne


Print Share Comment Cite Upload Translate Updates
APA

Robin Osborne | Sciencx (2024-06-04T08:34:23+00:00) Browser Super Powers: getUserMedia. Retrieved from https://www.scien.cx/2024/06/04/browser-super-powers-getusermedia/

MLA
" » Browser Super Powers: getUserMedia." Robin Osborne | Sciencx - Tuesday June 4, 2024, https://www.scien.cx/2024/06/04/browser-super-powers-getusermedia/
HARVARD
Robin Osborne | Sciencx Tuesday June 4, 2024 » Browser Super Powers: getUserMedia., viewed ,<https://www.scien.cx/2024/06/04/browser-super-powers-getusermedia/>
VANCOUVER
Robin Osborne | Sciencx - » Browser Super Powers: getUserMedia. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/04/browser-super-powers-getusermedia/
CHICAGO
" » Browser Super Powers: getUserMedia." Robin Osborne | Sciencx - Accessed . https://www.scien.cx/2024/06/04/browser-super-powers-getusermedia/
IEEE
" » Browser Super Powers: getUserMedia." Robin Osborne | Sciencx [Online]. Available: https://www.scien.cx/2024/06/04/browser-super-powers-getusermedia/. [Accessed: ]
rf:citation
» Browser Super Powers: getUserMedia | Robin Osborne | Sciencx | https://www.scien.cx/2024/06/04/browser-super-powers-getusermedia/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.