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 MediaSessionProperties
| Name | Description |
|---|---|
| IsCameraActive | Gets whether the page says that its camera is active (setCameraActive()). |
| IsMicrophoneActive | Gets whether the page says that its microphone is active (setMicrophoneActive()). |
| IsScreenshareActive | Gets whether the page says that it shares the screen (setScreenshareActive()). |
| Metadata | Gets or sets the metadata of the media that the page plays, or null. |
| PlaybackState | Gets or sets the playback state that the page declares (MediaSessionPlaybackState.None by default). |
| PositionState | Gets the position state set by MediaSession.SetPositionState, with all its members, or null when the page did not set it or cleared it. |
| PositionStateUpdateTime | Gets the time the position state was set (TimeProvider.GetUtcNow of the event loop), to compute the current position from the playback rate. |
| SupportedActions | Gets the actions that have a handler: the media controls of the platform show them. |
| Window | Gets the window of the media session. |
IsCameraActive
MediaSession.csIsMicrophoneActive
MediaSession.csMetadata
MediaSession.csGets or sets the metadata of the media that the page plays, or null.
public MediaMetadata? Metadata { get; set; }PlaybackState
MediaSession.csGets or sets the playback state that the page declares (MediaSessionPlaybackState.None by default).
public MediaSessionPlaybackState PlaybackState { get; set; }PositionState
MediaSession.csGets 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
PositionStateUpdateTime
MediaSession.csGets 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
SupportedActions
MediaSession.csGets the actions that have a handler: the media controls of the platform show them.
public IReadOnlyCollection<MediaSessionAction> SupportedActions { get; }Property value
Window
MediaSession.csMethods
| Name | Description |
|---|---|
| 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.csTriggers 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
detailsMediaSessionActionDetails- The action and its parameters.
Returns
bool Whether the action has a handler.
Exceptions
SetActionHandler(MediaSessionAction, MediaSessionActionHandler?)
MediaSession.csSets or removes the handler of an action (https://w3c.github.io/mediasession/#dom-mediasession-setactionhandler).
public void SetActionHandler(MediaSessionAction action, MediaSessionActionHandler? handler)Parameters
actionMediaSessionAction- The action.
handlerMediaSessionActionHandler?- The handler, or null to remove the handler of the action.
See also
SetCameraActive(bool)
MediaSession.csTells the user agent whether the camera of the page is active (https://w3c.github.io/mediasession/#dom-mediasession-setcameraactive).
SetMicrophoneActive(bool)
MediaSession.csTells the user agent whether the microphone of the page is active (https://w3c.github.io/mediasession/#dom-mediasession-setmicrophoneactive).
SetPositionState(MediaPositionState?)
MediaSession.csSets 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
stateMediaPositionState?- 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.