Skip to main content

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โ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
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
  1. Chrome: If you need this capability before version 53, refer to navigator.webkitGetUserMedia, a prefixed form of the deprecated navigator.getUserMedia API.
  2. Firefox: If you need this capability before version 36, refer to navigator.mozGetUserMedia, a prefixed form of the deprecated navigator.getUserMedia API.
  3. 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.
  4. 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.
  5. 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.
  6. Firefox: enumerateDevices() enumerates both input and output devices. Previously only input devices were returned.
  7. 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 check
  • getMediaDevices() โ€” lists available input/output devices
  • getUserMedia(constraints) โ€” requests camera/microphone access, returns a MediaStream
  • stopMediaStream(stream) โ€” stops all tracks in a MediaStream
  • getMediaPermissionStatus(name) โ€” checks 'camera' or 'microphone' permission status

See the API reference for full signatures.