MediaDevices Class

The media devices of a window (https://w3c.github.io/mediacapture-main/#mediadevices, navigator.mediaDevices): the cameras and the microphones of the MediaCaptureBackend of the browsing context (BrowsingContextOptions.MediaCapture), and the capture of the screen.

public sealed class MediaDevices : EventTarget

Remarks

The capture needs a secure context, a document allowed to use the camera and microphone features (display-capture for the screen), and the camera and microphone permissions (see BrowsingContextOptions.Permissions and BrowsingContextOptions.RequestPermission). The identifiers of the devices depend on the origin, and the document only sees them (and all the devices) once it captured a device of the kind.

See also

Inheritance
EventTarget MediaDevices

Properties

NameDescription
DefaultSemanticsGets the default semantics of MediaDevices.GetUserMedia: the user agent chooses the device (https://w3c.github.io/mediacapture-extensions/#dom-mediadevices-defaultsemantics).
OnDeviceChangeGets or sets the event handler of the devicechange event.

DefaultSemantics

MediaDevices.cs

Gets the default semantics of MediaDevices.GetUserMedia: the user agent chooses the device (https://w3c.github.io/mediacapture-extensions/#dom-mediadevices-defaultsemantics).

public GetUserMediaSemantics DefaultSemantics { get; }

OnDeviceChange

MediaDevices.cs

Gets or sets the event handler of the devicechange event.

public Action<Event>? OnDeviceChange { get; set; }

Methods

NameDescription
EnumerateDevices()Lists the media devices (https://w3c.github.io/mediacapture-main/#dom-mediadevices-enumeratedevices): the microphones, then the cameras, with their identifiers and labels once the document captured a device of the kind; otherwise at most one device of each kind, without details. The task completes when the document is visible.
GetDisplayMedia(DisplayMediaStreamOptions?)Requests the capture of a display surface (https://w3c.github.io/mediacapture-screen-share/#dom-mediadevices-getdisplaymedia): the user chooses a screen, a window or a tab (MediaCaptureBackend.SelectDisplaySurface). The window must have transient activation, which the call consumes. The task fails with a TypeErrorException for invalid options (no video, an advanced or a required constraint), an InvalidStateError without user activation or when the controller is already used, a NotAllowedError when the user cancels or the feature is not allowed, and an OverconstrainedError when the constraints cannot be satisfied.
GetSupportedConstraints()Returns the constrainable properties that the library supports (https://w3c.github.io/mediacapture-main/#dom-mediadevices-getsupportedconstraints).
GetUserMedia(MediaStreamConstraints?)Requests the capture of a camera and/or a microphone (https://w3c.github.io/mediacapture-main/#dom-mediadevices-getusermedia): the devices that satisfy the constraints best are started, once the user granted the permissions. The task fails with a TypeErrorException when no media is requested, a NotAllowedError when the permission is denied or the feature is not allowed, a NotFoundError when there is no device of the kind, an OverconstrainedError when no device satisfies the required constraints, and a NotReadableError when the device cannot be started.
SetCaptureHandleConfig(CaptureHandleConfig?)Sets the capture handle that the page exposes to the applications that capture it (https://w3c.github.io/mediacapture-handle/identity/#dom-mediadevices-setcapturehandleconfig).

EnumerateDevices()

MediaDevices.cs

Lists the media devices (https://w3c.github.io/mediacapture-main/#dom-mediadevices-enumeratedevices): the microphones, then the cameras, with their identifiers and labels once the document captured a device of the kind; otherwise at most one device of each kind, without details. The task completes when the document is visible.

public Task<IReadOnlyList<MediaDeviceInfo>> EnumerateDevices()

GetDisplayMedia(DisplayMediaStreamOptions?)

DisplayMedia.cs

Requests the capture of a display surface (https://w3c.github.io/mediacapture-screen-share/#dom-mediadevices-getdisplaymedia): the user chooses a screen, a window or a tab (MediaCaptureBackend.SelectDisplaySurface). The window must have transient activation, which the call consumes. The task fails with a TypeErrorException for invalid options (no video, an advanced or a required constraint), an InvalidStateError without user activation or when the controller is already used, a NotAllowedError when the user cancels or the feature is not allowed, and an OverconstrainedError when the constraints cannot be satisfied.

public Task<MediaStream> GetDisplayMedia(DisplayMediaStreamOptions? options = null)

GetSupportedConstraints()

MediaDevices.cs

Returns the constrainable properties that the library supports (https://w3c.github.io/mediacapture-main/#dom-mediadevices-getsupportedconstraints).

public MediaTrackSupportedConstraints GetSupportedConstraints()

GetUserMedia(MediaStreamConstraints?)

MediaDevices.cs

Requests the capture of a camera and/or a microphone (https://w3c.github.io/mediacapture-main/#dom-mediadevices-getusermedia): the devices that satisfy the constraints best are started, once the user granted the permissions. The task fails with a TypeErrorException when no media is requested, a NotAllowedError when the permission is denied or the feature is not allowed, a NotFoundError when there is no device of the kind, an OverconstrainedError when no device satisfies the required constraints, and a NotReadableError when the device cannot be started.

public Task<MediaStream> GetUserMedia(MediaStreamConstraints? constraints = null)

Parameters

constraints MediaStreamConstraints?
The media to capture and their constraints.

Returns

Task<MediaStream>

See also

SetCaptureHandleConfig(CaptureHandleConfig?)

CaptureHandle.cs

Sets the capture handle that the page exposes to the applications that capture it (https://w3c.github.io/mediacapture-handle/identity/#dom-mediadevices-setcapturehandleconfig).

public void SetCaptureHandleConfig(CaptureHandleConfig? config = null)

Parameters

config CaptureHandleConfig?
The configuration.

Exceptions

TypeErrorException
The handle is longer than 1024 characters.
DomException
A NotSupportedError: a permitted origin is invalid, or * is not alone; an InvalidStateError: the document is not the document of a top-level browsing context.

See also