MediaSession Class

The media session of a window (https://w3c.github.io/mediasession/#the-mediasession-interface, navigator.mediaSession): what the page plays (its metadata, its playback state and its position), and the actions that the media controls of the platform can trigger. The host is notified of the changes with MediaSessionOptions.SessionChanged, and triggers the actions with MediaSession.HandleAction.

public sealed class MediaSession

Properties

NameDescription
IsCameraActiveGets whether the page says that its camera is active (setCameraActive()).
IsMicrophoneActiveGets whether the page says that its microphone is active (setMicrophoneActive()).
IsScreenshareActiveGets whether the page says that it shares the screen (setScreenshareActive()).
MetadataGets or sets the metadata of the media that the page plays, or null.
PlaybackStateGets or sets the playback state that the page declares (MediaSessionPlaybackState.None by default).
PositionStateGets the position state set by MediaSession.SetPositionState, with all its members, or null when the page did not set it or cleared it.
PositionStateUpdateTimeGets the time the position state was set (TimeProvider.GetUtcNow of the event loop), to compute the current position from the playback rate.
SupportedActionsGets the actions that have a handler: the media controls of the platform show them.
WindowGets the window of the media session.

IsCameraActive

MediaSession.cs

Gets whether the page says that its camera is active (setCameraActive()).

public bool IsCameraActive { get; }

Property value

bool

IsMicrophoneActive

MediaSession.cs

Gets whether the page says that its microphone is active (setMicrophoneActive()).

public bool IsMicrophoneActive { get; }

Property value

bool

IsScreenshareActive

MediaSession.cs

Gets whether the page says that it shares the screen (setScreenshareActive()).

public bool IsScreenshareActive { get; }

Property value

bool

Gets or sets the metadata of the media that the page plays, or null.

public MediaMetadata? Metadata { get; set; }

PlaybackState

MediaSession.cs

Gets or sets the playback state that the page declares (MediaSessionPlaybackState.None by default).

public MediaSessionPlaybackState PlaybackState { get; set; }

PositionState

MediaSession.cs

Gets the position state set by MediaSession.SetPositionState, with all its members, or null when the page did not set it or cleared it.

public MediaPositionState? PositionState { get; }

Property value

MediaPositionState?

PositionStateUpdateTime

MediaSession.cs

Gets the time the position state was set (TimeProvider.GetUtcNow of the event loop), to compute the current position from the playback rate.

public DateTimeOffset PositionStateUpdateTime { get; }

Property value

DateTimeOffset

SupportedActions

MediaSession.cs

Gets the actions that have a handler: the media controls of the platform show them.

public IReadOnlyCollection<MediaSessionAction> SupportedActions { get; }

Gets the window of the media session.

public Window Window { get; }

Property value

Window

Methods

NameDescription
HandleAction(MediaSessionActionDetails)Triggers an action of the media session, as the media controls of the platform do (https://w3c.github.io/mediasession/#handle-media-session-action): the handler of the action is invoked by a task of the event loop of the window. Call it from the thread of the event loop.
SetActionHandler(MediaSessionAction, MediaSessionActionHandler?)Sets or removes the handler of an action (https://w3c.github.io/mediasession/#dom-mediasession-setactionhandler).
SetCameraActive(bool)Tells the user agent whether the camera of the page is active (https://w3c.github.io/mediasession/#dom-mediasession-setcameraactive).
SetMicrophoneActive(bool)Tells the user agent whether the microphone of the page is active (https://w3c.github.io/mediasession/#dom-mediasession-setmicrophoneactive).
SetPositionState(MediaPositionState?)Sets the position of the playback (https://w3c.github.io/mediasession/#dom-mediasession-setpositionstate), or clears it when the state is null or empty. The position defaults to 0, and the playback rate to 1.
SetScreenshareActive(bool)Tells the user agent whether the page shares the screen (https://w3c.github.io/mediasession/#dom-mediasession-setscreenshareactive).

HandleAction(MediaSessionActionDetails)

MediaSession.cs

Triggers an action of the media session, as the media controls of the platform do (https://w3c.github.io/mediasession/#handle-media-session-action): the handler of the action is invoked by a task of the event loop of the window. Call it from the thread of the event loop.

public bool HandleAction(MediaSessionActionDetails details)

Parameters

details MediaSessionActionDetails
The action and its parameters.

Returns

bool Whether the action has a handler.

Exceptions

ArgumentException
MediaSessionAction.SeekTo has no MediaSessionActionDetails.SeekTime.

SetActionHandler(MediaSessionAction, MediaSessionActionHandler?)

MediaSession.cs

Sets or removes the handler of an action (https://w3c.github.io/mediasession/#dom-mediasession-setactionhandler).

public void SetActionHandler(MediaSessionAction action, MediaSessionActionHandler? handler)

Parameters

action MediaSessionAction
The action.
handler MediaSessionActionHandler?
The handler, or null to remove the handler of the action.

See also

SetCameraActive(bool)

MediaSession.cs

Tells the user agent whether the camera of the page is active (https://w3c.github.io/mediasession/#dom-mediasession-setcameraactive).

public Task SetCameraActive(bool active)

Parameters

active bool
Whether the camera is active (not muted).

Returns

Task

See also

SetMicrophoneActive(bool)

MediaSession.cs

Tells the user agent whether the microphone of the page is active (https://w3c.github.io/mediasession/#dom-mediasession-setmicrophoneactive).

public Task SetMicrophoneActive(bool active)

Parameters

active bool
Whether the microphone is active (not muted).

Returns

Task

See also

SetPositionState(MediaPositionState?)

MediaSession.cs

Sets the position of the playback (https://w3c.github.io/mediasession/#dom-mediasession-setpositionstate), or clears it when the state is null or empty. The position defaults to 0, and the playback rate to 1.

public void SetPositionState(MediaPositionState? state = null)

Parameters

state MediaPositionState?
The position state.

Exceptions

TypeErrorException
The duration is missing or negative, the position is negative or greater than the duration, or the playback rate is 0.

See also

SetScreenshareActive(bool)

MediaSession.cs

Tells the user agent whether the page shares the screen (https://w3c.github.io/mediasession/#dom-mediasession-setscreenshareactive).

public Task SetScreenshareActive(bool active)

Parameters

active bool
Whether the screen sharing is active.

Returns

Task

See also