Skip to content

Commit

Permalink
Added option to prevent blocking of unexpected player events (#737)
Browse files Browse the repository at this point in the history
Co-authored-by: James Hunt <[email protected]>
  • Loading branch information
huntj88 and James Hunt authored Jan 16, 2024
1 parent 749b623 commit b37a6a9
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packages/live-share-media/src/MediaPlayerSynchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export class MediaPlayerSynchronizer extends EventEmitter {
private _onPlayerEvent: EventListener;
private _seekSuspension?: MediaSessionCoordinatorSuspension;
private _viewOnly = false;
private _blockUnexpectedPlayerEvents = true;
private _expectedPlaybackState: ExtendedMediaSessionPlaybackState = "none";
private _metadata: ExtendedMediaMetadata | null = null;
private _trackData: object | null = null;
Expand Down Expand Up @@ -171,7 +172,10 @@ export class MediaPlayerSynchronizer extends EventEmitter {
case "playing":
// Handle case for YouTube player where user can pause/play video by clicking on it.
// - Videos don't always start at 0.0 seconds.
if (this._expectedPlaybackState != "playing") {
if (
this._blockUnexpectedPlayerEvents &&
this._expectedPlaybackState != "playing"
) {
if (
this._mediaSession.coordinator.canPlayPause &&
this._player.currentTime < 1.0
Expand All @@ -192,6 +196,7 @@ export class MediaPlayerSynchronizer extends EventEmitter {
// block play if player state is playing when expected synced state is paused and coordinator is not suspended.
// needed because cannot tell if its a user initiated event, so disallow play
if (
this._blockUnexpectedPlayerEvents &&
this._expectedPlaybackState === "paused" &&
!this._mediaSession.coordinator.isSuspended
) {
Expand All @@ -201,6 +206,7 @@ export class MediaPlayerSynchronizer extends EventEmitter {
// block play if player state is playing when expected synced state is none and coordinator is not suspended.
// needed because user who is not in control should not be able to start, so disallow play
if (
this._blockUnexpectedPlayerEvents &&
this._expectedPlaybackState === "none" &&
!this._mediaSession.coordinator.isSuspended
) {
Expand All @@ -211,6 +217,7 @@ export class MediaPlayerSynchronizer extends EventEmitter {
// block pause if player state is paused when expected synced state is playing and coordinator is not suspended.
// needed because cannot tell if its a user initiated event, so disallow pause
if (
this._blockUnexpectedPlayerEvents &&
this._expectedPlaybackState === "playing" &&
!this._mediaSession.coordinator.isSuspended &&
// some players have ended, but emit a pause event immediately before ended event. Do not play if ended
Expand Down Expand Up @@ -411,6 +418,23 @@ export class MediaPlayerSynchronizer extends EventEmitter {
this.mediaSession.coordinator.canSetTrackData = !value;
}

/**
* @beta
* If true, pause and play actions will be ignored unless explicitly invoked from the corresponding play() and pause() functions.
*
* @remarks
* True is considered non-beta. If you set to false, this is an experimental beta behavior.
* Setting it to false may cause unintended side effects, such as local buffer events sending a pause event to remote clients.
* While setting this to false may prevent the need to add custom media controls for frameworks like video.js, we encourage you to be mindful of this and test thoroughly before using this setting.
*/
public get blockUnexpectedPlayerEvents(): boolean {
return this._blockUnexpectedPlayerEvents;
}

public set blockUnexpectedPlayerEvents(value: boolean) {
this._blockUnexpectedPlayerEvents = value;
}

/**
* Volume limiter used to temporarily reduce the videos volume when someone speaks in a meeting.
*/
Expand Down

0 comments on commit b37a6a9

Please sign in to comment.