Navigator: getUserMedia() method

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The deprecated Navigator.getUserMedia() method prompts the user for permission to use up to one video input device (such as a camera or shared screen) and up to one audio input device (such as a microphone) as the source for a MediaStream.

If permission is granted, a MediaStream whose video and/or audio tracks come from those devices is delivered to the specified success callback. If permission is denied, no compatible input devices exist, or any other error condition occurs, the error callback is executed with an object describing what went wrong. If the user instead doesn't make a choice at all, neither callback is executed.

Note: This is a legacy method. Please use the newer navigator.mediaDevices.getUserMedia() instead. While technically not deprecated, this old callback version is marked as such, since the specification strongly encourages using the newer promise returning version.

Syntax

js
getUserMedia(constraints, successCallback, errorCallback)

Parameters

constraints

An object specifying the types of media to request, along with any requirements for each type. For details, see the constraints section under the modern MediaDevices.getUserMedia() method, as well as the article Capabilities, constraints, and settings.

successCallback

A function which is invoked when the request for media access is approved. The function is called with one parameter: the MediaStream object that contains the media stream. Your callback can then assign the stream to the desired object (such as an <audio> or <video> element), as shown in the following example:

js
function successCallback(stream) {
  const video = document.querySelector("video");
  video.srcObject = stream;
  video.onloadedmetadata = (e) => {
    // Do something with the video here.
  };
}
errorCallback

When the call fails, the function specified in the errorCallback is invoked with an object as its sole argument; this object is modeled on DOMException.

Return value

None (undefined).

Examples

Width and height

Here's an example of using getUserMedia(), including code to cope with various browsers' prefixes. Note that this is the deprecated way of doing it: See the Examples section under the MediaDevices.getUserMedia() for modern examples.

js
navigator.getUserMedia =
  navigator.getUserMedia ||
  navigator.webkitGetUserMedia ||
  navigator.mozGetUserMedia;

if (navigator.getUserMedia) {
  navigator.getUserMedia(
    { audio: true, video: { width: 1280, height: 720 } },
    (stream) => {
      const video = document.querySelector("video");
      video.srcObject = stream;
      video.onloadedmetadata = (e) => {
        video.play();
      };
    },
    (err) => {
      console.error(`The following error occurred: ${err.name}`);
    },
  );
} else {
  console.log("getUserMedia not supported");
}

Specifications

Specification
Media Capture and Streams
# dom-navigator-getusermedia

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Node.js
getUserMedia
Deprecated
Chrome – Full support
Chrome 21 (Release date: 2012-07-31)
prefix
prefix Implemented with the vendor prefix: webkit
Chrome – Full support
Chrome 53 (Release date: 2016-08-31)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
prefix
prefix Implemented with the vendor prefix: webkit
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 17 (Release date: 2012-11-20)
prefix
prefix Implemented with the vendor prefix: moz
Opera – No support
Opera 12 – 12.1 (Release date: 2012-06-14)
unknown Support unknown
Opera – Full support
Opera 15 (Release date: 2013-07-02)
prefix
prefix Implemented with the vendor prefix: webkit
Opera – Full support
Opera 40 (Release date: 2016-09-20)
footnote Full support
Safari – No support
Safari 11 – 11.1 (Release date: 2017-09-19)
footnote Removed in 12 and later
Chrome Android – Full support
Chrome Android 25 (Release date: 2013-02-27)
prefix
prefix Implemented with the vendor prefix: webkit
Chrome Android – Full support
Chrome Android 53 (Release date: 2016-09-07)
footnote Full support
Firefox for Android – Full support
Firefox for Android 24 (Release date: 2013-09-17)
prefix
prefix Implemented with the vendor prefix: moz
Opera Android – No support
Opera Android 12 – 12.1 (Release date: 2012-02-25)
unknown Support unknown
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
prefix
prefix Implemented with the vendor prefix: webkit
Opera Android – Full support
Opera Android 41 (Release date: 2016-10-25)
footnote Full support
Safari on iOS – No support
Safari on iOS 11 – 11.3 (Release date: 2017-09-19)
footnote Removed in 12 and later
Samsung Internet – Full support
Samsung Internet 1.5 (Release date: 2013-09-25)
prefix
prefix Implemented with the vendor prefix: webkit
Samsung Internet – Full support
Samsung Internet 6 (Release date: 2017-08-23)
footnote Full support
WebView Android – Full support
WebView Android 40 (Release date: 2015-01-21)
prefix
prefix Implemented with the vendor prefix: webkit
WebView Android – Full support
WebView Android 53 (Release date: 2016-09-07)
footnote Full support
WebView on iOS – No support
WebView on iOS 11 – 11.3 (Release date: 2017-09-19)
footnote Removed in 12 and later
Node.js – No support
Node.js
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Deprecated. Not for use in new websites.
Requires a vendor prefix or different name for use.
Has more compatibility info.

See also