Skip to content

Commit

Permalink
feat(player): new changeDuration on remote control
Browse files Browse the repository at this point in the history
ref #1330
  • Loading branch information
mihar-22 committed Jul 9, 2024
1 parent 6b3457d commit 71d4594
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
10 changes: 10 additions & 0 deletions packages/vidstack/src/core/api/media-request-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ScreenOrientationLockType } from '../../foundation/orientation/typ
export interface MediaRequestEvents {
'media-airplay-request': MediaAirPlayRequestEvent;
'media-audio-track-change-request': MediaAudioTrackChangeRequestEvent;
'media-duration-change-request': MediaDurationChangeRequestEvent;
'media-enter-fullscreen-request': MediaEnterFullscreenRequestEvent;
'media-exit-fullscreen-request': MediaExitFullscreenRequestEvent;
'media-enter-pip-request': MediaEnterPIPRequestEvent;
Expand Down Expand Up @@ -93,6 +94,15 @@ export type MediaFullscreenRequestTarget = 'prefer-media' | 'media' | 'provider'
*/
export interface MediaAudioTrackChangeRequestEvent extends DOMEvent<number> {}

/**
* Fired when requesting to change the length of the media. The event `detail` specifies the
* new length in seconds.
*
* @bubbles
* @composed
*/
export interface MediaDurationChangeRequestEvent extends DOMEvent<number> {}

/**
* Fired when requesting media to enter fullscreen. The event `detail` can specify the
* fullscreen target, which can be the media or provider (defaults to `prefer-media`).
Expand Down
9 changes: 9 additions & 0 deletions packages/vidstack/src/core/state/media-request-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,15 @@ export class MediaRequestManager extends MediaPlayerController implements MediaR
}
}

['media-duration-change-request'](event: RE.MediaDurationChangeRequestEvent) {
const { providedDuration } = this.$state;
providedDuration.set(event.detail);
this.dispatch('duration-change', {
detail: event.detail,
trigger: event,
});
}

['media-audio-track-change-request'](event: RE.MediaAudioTrackChangeRequestEvent) {
const { logger, audioTracks } = this._media;

Expand Down
7 changes: 6 additions & 1 deletion packages/vidstack/src/core/state/media-state-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,18 @@ export class MediaStateManager extends MediaPlayerController {
}

['duration-change'](event: ME.MediaDurationChangeEvent) {
const { live, intrinsicDuration, ended } = this.$state,
const { live, intrinsicDuration, providedDuration, ended } = this.$state,
time = event.detail;

if (!live()) {
const duration = !Number.isNaN(time) ? time : 0;
intrinsicDuration.set(duration);
if (ended()) this._onEndPrecisionChange(event);
}

if (providedDuration()) {
event.stopImmediatePropagation();
}
}

['progress'](event: ME.MediaProgressEvent) {
Expand Down
6 changes: 4 additions & 2 deletions packages/vidstack/src/core/state/media-state-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ export class MediaStateSync extends MediaPlayerController {
}

private _watchDuration() {
const { providedDuration } = this.$state;
providedDuration.set(this.$props.duration());
const { duration } = this.$props;
this.dispatch('media-duration-change-request', {
detail: duration(),
});
}

private _watchPlaysInline() {
Expand Down
12 changes: 12 additions & 0 deletions packages/vidstack/src/core/state/remote-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ export class MediaRemoteControl {
this._dispatchRequest('media-live-edge-request', trigger);
}

/**
* Dispatch a request to update the length of the media in seconds.
*
* @example
* ```ts
* remote.changeDuration(100); // 100 seconds
* ```
*/
changeDuration(duration: number, trigger?: Event) {
this._dispatchRequest('media-duration-change-request', trigger, duration);
}

/**
* Dispatch a request to update the media volume to the given `volume` level which is a value
* between 0 and 1.
Expand Down

0 comments on commit 71d4594

Please sign in to comment.