Table of Contents

Class MediaDevices

Namespace
CSharpToJavaScript.APIs.JS
Assembly
CSharpToJavaScript.dll

The MediaDevices interface of the {{domxref("Media Capture and Streams API", "", "", "nocode")}} provides access to connected media input devices like cameras and microphones, as well as screen sharing. In essence, it lets you obtain access to any hardware source of media data.

[Value("MediaDevices")]
public class MediaDevices : EventTarget
Inheritance
MediaDevices
Inherited Members

Remarks

-Media Capture and Streams API: The API this interface is part of.
-Screen Capture API: The API defining the GetDisplayMedia(DisplayMediaStreamOptions) method.
-WebRTC API
-MediaDevices: Returns a reference to a MediaDevices object that can be used to access devices.
-CameraCaptureJS: HTML video capture and playback using MediaDevices and the MediaStream Recording API
-OpenLang: HTML video language lab web application using MediaDevices and the MediaStream Recording API for video recording

See also on MDN

Constructors

MediaDevices()

public MediaDevices()

Properties

Oncaptureaction

[Value("oncaptureaction")]
public EventHandlerNonNull Oncaptureaction { get; set; }

Property Value

EventHandlerNonNull

Ondevicechange

[Value("ondevicechange")]
public EventHandlerNonNull Ondevicechange { get; set; }

Property Value

EventHandlerNonNull

Methods

EnumerateDevices()

The enumerateDevices() method of the MediaDevices interface requests a list of the currently available media input and output devices, such as microphones, cameras, headsets, and so forth.
The returned {{jsxref("Promise")}} is resolved with an array of MediaDeviceInfo objects describing the devices.

[Value("enumerateDevices")]
public Task<List<MediaDeviceInfo>> EnumerateDevices()

Returns

Task<List<MediaDeviceInfo>>

A {{jsxref("Promise")}} that is fulfilled with an array of MediaDeviceInfo objects.
Each object in the array describes one of the available media input and output devices.
The order is significant — the default capture devices will be listed first.Other than default devices, only devices for which permission has been granted are &quot;available&quot;.If the media device is an input device, an InputDeviceInfo object will be returned instead.If enumeration fails, the promise is rejected.

Remarks

The returned list will omit any devices that are blocked by the document Permission Policy: microphone, camera, speaker-selection (for output devices), and so on.
Access to particular non-default devices is also gated by the Permissions API, and the list will omit devices for which the user has not granted explicit permission.

-GetUserMedia(MediaStreamConstraints)
-WebRTC - the introductory page to the API
-Media Capture and Streams API - the API for the media stream objects
-Taking webcam photos - a
tutorial on using getUserMedia() for taking photos rather than video.

See also on MDN

GetDisplayMedia(DisplayMediaStreamOptions)

The getDisplayMedia() method of the MediaDevices interface prompts the user to select and
grant permission to capture the contents of a display or portion thereof (such as a window) as a MediaStream.

[Value("getDisplayMedia")]
public Task<MediaStream> GetDisplayMedia(DisplayMediaStreamOptions options = null)

Parameters

options DisplayMediaStreamOptions

Returns

Task<MediaStream>

A {{jsxref("Promise")}} that resolves to a MediaStream containing a
video track whose contents come from a user-selected screen area, as well as an optional
audio track.

NOTE
Browser support for audio tracks varies, both in terms of whether or not they&apos;re supported at all by the media recorder and in terms of the audio sources supported. Check the compatibility table for details for each browser.

Remarks

The resulting stream can then be
recorded using the MediaStream Recording API or transmitted as part of a WebRTC session.

See Using the Screen Capture API for more details and an example.

-Screen Capture API
-Using the Screen Capture API
-Media Capture and Streams API
-WebRTC API
-GetUserMedia(MediaStreamConstraints): Capturing media from a
camera and/or microphone

See also on MDN

GetSupportedConstraints()

The getSupportedConstraints() method of the MediaDevices interface returns an object based on the MediaTrackSupportedConstraints dictionary, whose member fields each specify one of the constrainable properties the {{Glossary("user agent")}} understands.

[Value("getSupportedConstraints")]
public MediaTrackSupportedConstraints GetSupportedConstraints()

Returns

MediaTrackSupportedConstraints

A new object based on the MediaTrackSupportedConstraints dictionary listing the constraints supported by the user agent.
Because only constraints supported by the user agent are included in the list, each of these Boolean properties has the value true.

Remarks

GetUserMedia(MediaStreamConstraints)

The getUserMedia() method of the MediaDevices interface prompts the user for permission to use a media input which produces a MediaStream with tracks containing the requested types of media.

[Value("getUserMedia")]
public Task<MediaStream> GetUserMedia(MediaStreamConstraints constraints = null)

Parameters

constraints MediaStreamConstraints

Returns

Task<MediaStream>

A {{jsxref("Promise")}} whose fulfillment handler receives a MediaStream
object when the requested media has successfully been obtained.

Remarks

That stream can include, for example, a video track (produced by either a hardware or virtual video source such as a camera, video recording device, screen sharing service, and so forth), an audio track (similarly, produced by a physical or virtual audio source like a microphone, A/D converter, or the like), and possibly other track types.

It returns a {{jsxref("Promise")}} that resolves to a MediaStream object.
If the user denies permission, or matching media is not available, then the promise is rejected with NotAllowedError or NotFoundError DOMException respectively.

NOTE

It&apos;s possible for the returned promise to neither resolve nor reject, as the user is not required to make a choice at all and may ignore the request.

-The older Navigator.GetUserMedia legacy API
-EnumerateDevices(): Listing available media devices
-{{domxref(&quot;WebRTC API&quot;, &quot;&quot;, &quot;&quot;, &quot;nocode&quot;)}}
-{{domxref(&quot;Media Capture and Streams API&quot;, &quot;&quot;, &quot;&quot;, &quot;nocode&quot;)}}
-{{domxref(&quot;Screen Capture API&quot;, &quot;&quot;, &quot;&quot;, "nocode")}}: Capturing screen contents as a MediaStream
-GetDisplayMedia(DisplayMediaStreamOptions): Getting a stream containing screen contents
-{{domxref(&quot;Media Capture and Streams API/Taking Still Photos&quot;, &quot;Taking webcam photos&quot;, &quot;&quot;, &quot;nocode&quot;)}}: A tutorial on using getUserMedia() to take still photos rather than video

See also on MDN

GetViewportMedia(DisplayMediaStreamOptions)

[Value("getViewportMedia")]
public Task<MediaStream> GetViewportMedia(DisplayMediaStreamOptions options = null)

Parameters

options DisplayMediaStreamOptions

Returns

Task<MediaStream>

SelectAudioOutput(AudioOutputOptions)

NOTE
Experimental
The selectAudioOutput() method of the MediaDevices interface prompts the user to select an audio output device, such as a speaker or headset. If the user selects a device, the method grants user permission to use the selected device as an audio output sink.
[Value("selectAudioOutput")]
public Task<MediaDeviceInfo> SelectAudioOutput(AudioOutputOptions options = null)

Parameters

options AudioOutputOptions

Returns

Task<MediaDeviceInfo>

A {{ jsxref("Promise") }} that is fulfilled with a MediaDeviceInfo object that describes the audio output device selected by the user.

Remarks

Following selection, if the device is available it can be enumerated using EnumerateDevices() and set as the audio output sink using SetSinkId(string).

On success, the returned {{jsxref("Promise")}} is resolved with a MediaDeviceInfo describing the selected device.

-SetSinkId(string)
-SinkId
-WebRTC - the introductory page to the API

See also on MDN

SetCaptureHandleConfig(CaptureHandleConfig)

[Value("setCaptureHandleConfig")]
public GlobalObject.Undefined SetCaptureHandleConfig(CaptureHandleConfig config = null)

Parameters

config CaptureHandleConfig

Returns

GlobalObject.Undefined

SetSupportedCaptureActions(List<string>)

[Value("setSupportedCaptureActions")]
public GlobalObject.Undefined SetSupportedCaptureActions(List<string> actions)

Parameters

actions List<string>

Returns

GlobalObject.Undefined