mediadevices
Enumerate media devices and request camera/microphone access via the MediaDevices API.
Import: @rtorcato/browser-common/mediadevices
๐ MDN: MediaDevices ยท ๐ caniuse: stream
getUserMedia needs a secure context (HTTPS/localhost) and prompts for permission.
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
MediaDevices.getUserMedia | โ 53+ [1] | โ 12+ | โ 36+ [2,3,4,5] | โ 11+ | โ 11+ | Widely available |
MediaDevices.enumerateDevices | โ 47+ | โ 12+ | โ 116+ [6,7] | โ 11+ | โ 11+ | Widely available |
- Chrome: If you need this capability before version 53, refer to navigator.webkitGetUserMedia, a prefixed form of the deprecated navigator.getUserMedia API.
- Firefox: If you need this capability before version 36, refer to navigator.mozGetUserMedia, a prefixed form of the deprecated navigator.getUserMedia API.
- Firefox: Before Firefox 55, getUserMedia() incorrectly returns NotSupportedError when the list of constraints specified is empty, or has all constraints set to false. Starting in Firefox 55, this situation now correctly calls the failure handler with a TypeError.
- Firefox: When using the Firefox-specific video constraint called mediaSource to request display capture, Firefox 66 and later consider values of screen and window to both cause a list of screens and windows to be shown.
- Firefox: Starting in Firefox 66, getUserMedia() can no longer be used in sandboxed <iframe>s or data URLs entered in the address bar by the user.
- Firefox: enumerateDevices() enumerates both input and output devices. Previously only input devices were returned.
- Firefox: The promise returned by enumerateDevices() only resolves when the document has focus. See bug 1818588.
Needs: secure context (HTTPS)
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: MediaDevices.getUserMedia ยท MediaDevices.enumerateDevices
Exampleโ
import {
isMediaDevicesAvailable,
getMediaDevices,
getUserMedia,
stopMediaStream,
} from '@rtorcato/browser-common/mediadevices'
if (isMediaDevicesAvailable()) {
const devices = await getMediaDevices()
const cams = devices.filter((d) => d.kind === 'videoinput')
const stream = await getUserMedia({ video: true, audio: true })
// ... use stream
stopMediaStream(stream)
}
Exportsโ
isMediaDevicesAvailable()โ feature checkgetMediaDevices()โ lists available input/output devicesgetUserMedia(constraints)โ requests camera/microphone access, returns aMediaStreamstopMediaStream(stream)โ stops all tracks in aMediaStreamgetMediaPermissionStatus(name)โ checks'camera'or'microphone'permission status
See the API reference for full signatures.