navigator.getUserMedia(constraints, success, error) is the legacy callback way to ask for a camera and/or microphone MediaStream (MDN). New code should use navigator.mediaDevices.getUserMedia() instead.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call navigator.getUserMedia. Start with this line.
Open the stream briefly. Same modern API as example 3.
Count tracks. Useful for logging / tests.
Call track.stop(). Turns off the camera light and releases hardware.
Also stop on leave. Clear srcObject when navigating away or closing a modal.
Compatibility
Browser Support
navigator.getUserMedia() is the Deprecated callback API. The modern replacement navigator.mediaDevices.getUserMedia() is Baseline Widely available (since Sept 2017) and requires a secure context. Prefer mediaDevices for all new JavaScript. Legacy prefixed names (webkitGetUserMedia / mozGetUserMedia) appear only in old code.
✓ Deprecated legacy · Modern Baseline
Navigator.getUserMedia()
Legacy callbacks for camera/mic — migrate to mediaDevices.getUserMedia().
95%Modern mediaDevices support
Google ChromemediaDevices · Baseline; legacy deprecated
Supported
Microsoft EdgemediaDevices · Baseline; legacy deprecated
Supported
Apple SafarimediaDevices · Baseline; legacy deprecated
Supported
OperamediaDevices · Baseline; legacy deprecated
Supported
Mozilla FirefoxmediaDevices · Baseline; legacy deprecated
Supported
Internet ExplorerNo standard mediaDevices.getUserMedia
Unavailable
mediaDevices.getUserMedia()Excellent
Bottom line: Teach the deprecated API for reading legacy code; ship mediaDevices.getUserMedia with HTTPS, permission UX, and track.stop().
Frequently Asked Questions
It is the old callback API that asks for permission to use the camera and/or microphone and delivers a MediaStream to a success callback, or an error object to an error callback.
No. MDN marks it Deprecated. Use navigator.mediaDevices.getUserMedia(), which returns a Promise and is the supported modern API.
navigator.mediaDevices.getUserMedia(constraints). Await the Promise, assign stream to video.srcObject, and stop tracks when finished.
Yes. getUserMedia is available only in secure contexts (HTTPS or localhost). In insecure pages, mediaDevices is often undefined.
An object describing which media you want, such as { audio: true, video: true } or more detailed video size settings like { video: { width: 1280, height: 720 } }.
Neither callback (legacy) nor the Promise (modern) may settle until the user chooses Allow or Block. Design UI that waits and offers a clear retry.