Skip to content

Commit

Permalink
Consider special cases of LOADING / RELOADING / STOPPED for play & pa…
Browse files Browse the repository at this point in the history
…use events
  • Loading branch information
peaBerberian committed Jun 13, 2023
1 parent f7e04ce commit 0218f54
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
9 changes: 5 additions & 4 deletions doc/api/Player_Events.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ The object emitted as the following properties:

Emitted when the `RxPlayer`'s `videoElement` is no longer considered paused.

This event is triggered when and if the [`play`](./Basic_Methods/play.md) method
has succeeded or when auto-play succeeded if the `autoPlay` `loadVideo` option has
been set to `true`.
This event is generally triggered when and if the
[`play`](./Basic_Methods/play.md) method has succeeded.

Note that this event can be sent even if the [player's state](./Player_States.md)
doesn't currently allow playback, for example when in the `"LOADING"` or
Expand All @@ -113,7 +112,9 @@ when no content is loading nor loaded.
Emitted when the `RxPlayer`'s `videoElement` is now considered paused.

This event is triggered when and if the [`pause`](./Basic_Methods/play.md) method
has succeeded or when the content has ended.
has succeeded, when the content has ended or due to other rare occurences: for
example if we could not automatically play after a `"LOADING"` or `"RELOADING"`
state due to [the browser's autoplay policies](https://developer.mozilla.org/en-US/docs/Web/Media/Autoplay_guide).

Note that this event can be sent even if the [player's state](./Player_States.md)
doesn't currently allow playback, for example when in the `"LOADING"` or
Expand Down
50 changes: 46 additions & 4 deletions src/core/api/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,10 +946,52 @@ class Player extends EventEmitter<IPublicAPIEvent> {
}
};

emitPlayPauseEvents(videoElement,
() => this.trigger("play", null),
() => this.trigger("pause", null),
currentContentCanceller.signal);
/**
* `TaskCanceller` allowing to stop emitting `"play"` and `"pause"`
* events.
* `null` when such events are not emitted currently.
*/
let playPauseEventsCanceller : TaskCanceller | null = null;

/**
* Callback emitting `"play"` and `"pause`" events once the content is
* loaded, starting from the state indicated in argument.
* @param {boolean} willAutoPlay - If `false`, we're currently paused.
*/
const triggerPlayPauseEventsWhenReady = (willAutoPlay: boolean) => {
if (playPauseEventsCanceller !== null) {
playPauseEventsCanceller.cancel(); // cancel previous logic
playPauseEventsCanceller = null;
}
playerStateRef.onUpdate((val, stopListeningToStateUpdates) => {
if (!isLoadedState(val)) {
return; // content not loaded yet: no event
}
stopListeningToStateUpdates();
if (playPauseEventsCanceller !== null) {
playPauseEventsCanceller.cancel();
}
playPauseEventsCanceller = new TaskCanceller();
playPauseEventsCanceller.linkToSignal(currentContentCanceller.signal);
if (willAutoPlay !== !videoElement.paused) {
// paused status is not at the expected value on load: emit event
if (videoElement.paused) {
this.trigger("pause", null);
} else {
this.trigger("play", null);
}
}
emitPlayPauseEvents(videoElement,
() => this.trigger("play", null),
() => this.trigger("pause", null),
currentContentCanceller.signal);
}, { emitCurrentValue: false, clearSignal: currentContentCanceller.signal });
};

triggerPlayPauseEventsWhenReady(autoPlay);
initializer.addEventListener("reloadingMediaSource", (payload) => {
triggerPlayPauseEventsWhenReady(payload.autoPlay);
});

/**
* `TaskCanceller` allowing to stop emitting `"seeking"` and `"seeked"`
Expand Down

0 comments on commit 0218f54

Please sign in to comment.