navigator.mediaDevices returns a MediaDevices object for cameras, microphones, and screen sharing. Learn getUserMedia, enumerateDevices, secure context rules, five examples, and try-it labs.
01
Kind
Read-only property
02
Returns
MediaDevices
03
Baseline
Widely available
04
Context
Secure (HTTPS)
05
Key API
getUserMedia()
06
List
enumerateDevices()
Fundamentals
Introduction
Video calls, camera filters, and voice notes all start the same way: ask the browser for a live media stream from a camera or microphone.
navigator.mediaDevices is the modern entry point. MDN describes it as a singleton MediaDevices object — you usually call methods on it directly, such as navigator.mediaDevices.getUserMedia(). It belongs to the Media Capture and Streams API and pairs closely with WebRTC.
💡
Permission + HTTPS
Browsers only expose this API in a secure context. Live capture also needs the user’s permission. Always feature-detect, handle denial, and stop tracks when you are done.
Concept
Understanding the mediaDevices Property
Think of it as the browser’s “media hardware front desk”: list devices, request a stream, and react when hardware is plugged in or removed.
Value — the MediaDevices singleton.
getUserMedia(constraints) — returns a Promise for a MediaStream.
enumerateDevices() — lists audio/video input and output devices.
devicechange — event when devices are added or removed.
Also — related helpers like getDisplayMedia() for screen sharing (support varies).
Baseline — Widely available (MDN, since about September 2017); secure context required.
Foundation
📝 Syntax
General form of the property:
JavaScript
navigator.mediaDevices
Value
A MediaDevices object (usually used via its methods, not by reading fields).
Four facts to remember about navigator.mediaDevices.
Returns
MediaDevices
API entry
Baseline
widely
Since ~Sep 2017
Context
HTTPS
Secure required
Star method
getUserMedia
Live MediaStream
Compare
📋 mediaDevices vs legacy getUserMedia
navigator.mediaDevices
Legacy navigator.getUserMedia
Style
Promise-based modern API
Callback-based (deprecated path)
Entry
mediaDevices.getUserMedia()
navigator.getUserMedia / prefixed variants
Extras
enumerateDevices, devicechange, display capture
Capture only (limited)
Use today?
Yes — preferred
No — migrate away
Hands-On
Examples Gallery
Examples follow MDN MediaDevices patterns. Prefer Try It Yourself on HTTPS or localhost. Camera/mic demos need user permission.
📚 Getting Started
Detect the API and list connected devices safely.
Example 1 — Feature + Secure Context Check
Confirm mediaDevices exists and the page is in a secure context.
JavaScript
const lines = [
"secure: " + window.isSecureContext,
"mediaDevices: " + Boolean(navigator.mediaDevices)
];
if (!window.isSecureContext) {
lines.push("Serve over HTTPS (or localhost) to use mediaDevices");
} else if (!navigator.mediaDevices) {
lines.push("Secure, but MediaDevices API not exposed");
} else {
lines.push("Ready for getUserMedia / enumerateDevices");
}
console.log(lines.join("\n"));
got stream → video:Integrated Camera, audio:Microphone Array
(or NotAllowedError if the user denies permission)
How It Works
On success you get a MediaStream. Attach it with video.srcObject = stream in a real UI, and always stop() tracks when finished.
Example 4 — Listen for devicechange
Refresh your device list when the user plugs in a headset or webcam.
JavaScript
if (!navigator.mediaDevices) {
console.log("MediaDevices API missing");
} else {
navigator.mediaDevices.addEventListener("devicechange", function () {
console.log("devicechange — re-run enumerateDevices()");
});
console.log("Listening for devicechange…");
// Plug/unplug a USB mic to see the event in Try It Yourself
}
navigator.mediaDevices is Baseline Widely available (MDN: since about September 2017) but requires a secure context. Related helpers like getDisplayMedia may vary — feature-detect them separately.
✓ Baseline · Secure context
Navigator.mediaDevices
Entry point for camera, microphone, device lists, and related capture APIs on HTTPS pages.
UniversalWidely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
mediaDevicesExcellent
Bottom line: Use HTTPS, feature-detect mediaDevices, request permission with getUserMedia, and always stop tracks when finished.
Wrap Up
Conclusion
navigator.mediaDevices is the modern gateway to cameras, microphones, and related capture APIs. Serve over HTTPS, call getUserMedia with clear constraints, list devices with enumerateDevices, and stop tracks when you are done.
It is a read-only Navigator property that returns a MediaDevices object — the entry point for cameras, microphones, and related media input/output APIs such as getUserMedia() and enumerateDevices().
No. MDN marks it Baseline Widely available (since about September 2017). It is not Deprecated, Experimental, or Non-standard. Some related methods have varying support.
Yes in supporting browsers: mediaDevices is available only in secure contexts (HTTPS or localhost).
navigator.mediaDevices.getUserMedia(constraints) asks the user for permission and returns a MediaStream with live audio and/or video tracks — the foundation of video calls, filters, and recording.
Before permission is granted, enumerateDevices() often returns devices with empty label strings for privacy. After the user allows camera/mic access, labels usually appear.
No. It is read-only. You use methods on the MediaDevices object; you do not assign a new value to navigator.mediaDevices.
Did you know?
MDN notes you usually do not “read” mediaDevices for data — you call its members directly, like navigator.mediaDevices.getUserMedia(). The property is just the doorway to the MediaDevices singleton.