Skip to content

Commit

Permalink
fix(player/react): updating current time on page transitions throws
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Nov 30, 2023
1 parent 1b72abc commit ed8b646
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions packages/vidstack/src/components/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ export class MediaPlayer
}

private _queueMutedUpdate(muted: boolean) {
this.canPlayQueue._enqueue('muted', () => this._provider!.setMuted(muted));
this.canPlayQueue._enqueue('muted', () => {
if (this._provider) this._provider.setMuted(muted);
});
}

@prop
Expand All @@ -469,17 +471,17 @@ export class MediaPlayer

private _queueCurrentTimeUpdate(time: number) {
this.canPlayQueue._enqueue('currentTime', () => {
const adapter = this._provider;
if (time !== peek(this.$state.currentTime)) {
peek(() => {
const boundTime = Math.min(
Math.max(this.$state.seekableStart() + 0.1, time),
this.$state.seekableEnd() - 0.1,
);

if (Number.isFinite(boundTime)) adapter!.setCurrentTime(boundTime);
});
}
if (time === peek(this.$state.currentTime)) return;
peek(() => {
if (!this._provider) return;

const boundTime = Math.min(
Math.max(this.$state.seekableStart() + 0.1, time),
this.$state.seekableEnd() - 0.1,
);

if (Number.isFinite(boundTime)) this._provider.setCurrentTime(boundTime);
});
});
}

Expand All @@ -498,7 +500,9 @@ export class MediaPlayer

private _queueVolumeUpdate(volume: number) {
const clampedVolume = clampNumber(0, volume, 1);
this.canPlayQueue._enqueue('volume', () => this._provider!.setVolume(clampedVolume));
this.canPlayQueue._enqueue('volume', () => {
if (this._provider) this._provider.setVolume(clampedVolume);
});
}

@prop
Expand All @@ -515,18 +519,19 @@ export class MediaPlayer
}

private _queuePlaybackRateUpdate(rate: number) {
this.canPlayQueue._enqueue('rate', () => this._provider!.setPlaybackRate?.(rate));
this.canPlayQueue._enqueue('rate', () => {
if (this._provider) this._provider.setPlaybackRate?.(rate);
});
}

private _watchPlaysinline() {
this._queuePlaysinlineUpdate(this.$props.playsinline());
}

private _queuePlaysinlineUpdate(inline: boolean) {
this.canPlayQueue._enqueue(
'playsinline',
() => (this._provider as MediaProviderAdapter).setPlaysinline?.(inline),
);
this.canPlayQueue._enqueue('playsinline', () => {
if (this._provider) (this._provider as MediaProviderAdapter).setPlaysinline?.(inline);
});
}

/**
Expand Down

0 comments on commit ed8b646

Please sign in to comment.