From aa8bd5df97b765b0c611cdf39e6ad255592a1ae7 Mon Sep 17 00:00:00 2001 From: AntonChesnokov Date: Thu, 18 Dec 2025 19:38:33 +0400 Subject: [PATCH] fix(youtube-player): apply startSeconds with disablePlaceholder and autoplay Fixes a bug where the YouTube player ignored the startSeconds input when both disablePlaceholder and playerVars.autoplay were set. The player now correctly seeks to the specified start time using seekTo() when the video autoplays. The issue occurred because the code only checked the playVideo parameter to determine if seekTo should be called, but didn't account for videos that autoplay through playerVars.autoplay = 1. Fixes #32545 --- src/youtube-player/youtube-player.spec.ts | 16 ++++++++++++++++ src/youtube-player/youtube-player.ts | 14 +++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/youtube-player/youtube-player.spec.ts b/src/youtube-player/youtube-player.spec.ts index ab3c98b34212..064703f21201 100644 --- a/src/youtube-player/youtube-player.spec.ts +++ b/src/youtube-player/youtube-player.spec.ts @@ -679,6 +679,22 @@ describe('YoutubePlayer', () => { expect(playerCtorSpy).toHaveBeenCalled(); }); + it('should apply startSeconds when disablePlaceholder and autoplay are both set', () => { + testComponent.disablePlaceholder = true; + testComponent.playerVars = {autoplay: 1}; + testComponent.startSeconds = 30; + fixture.changeDetectorRef.markForCheck(); + fixture.detectChanges(); + + // Simulate player state being PLAYING (autoplay has started the video) + playerSpy.getPlayerState.and.returnValue(window.YT!.PlayerState.PLAYING); + events.onReady({target: playerSpy}); + + // Should use seekTo instead of cueVideoById when player is already playing + expect(playerSpy.seekTo).toHaveBeenCalledWith(30, true); + expect(playerSpy.cueVideoById).not.toHaveBeenCalled(); + }); + it('should allow for the placeholder image quality to be changed', () => { const placeholder = getPlaceholder(fixture); expect(placeholder.style.backgroundImage).toContain( diff --git a/src/youtube-player/youtube-player.ts b/src/youtube-player/youtube-player.ts index 46e472e03dd5..440a3eb0493a 100644 --- a/src/youtube-player/youtube-player.ts +++ b/src/youtube-player/youtube-player.ts @@ -628,11 +628,15 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy { const state = player.getPlayerState(); if (state === PlayerState.UNSTARTED || state === PlayerState.CUED || state == null) { this._cuePlayer(); - } else if (playVideo && this.startSeconds && this.startSeconds > 0) { - // We have to use `seekTo` when `startSeconds` are specified to simulate it playing from - // a specific time. The "proper" way to do it would be to either go through `cueVideoById` - // or `playerVars.start`, but at the time of writing both end up resetting the video - // to the state as if the user hasn't interacted with it. + } else if ( + (playVideo || this.playerVars?.autoplay === 1) && + this.startSeconds && + this.startSeconds > 0 + ) { + // We have to use `seekTo` when `startSeconds` are specified with a playing video + // (either from user interaction or autoplay). The "proper" way to do it would be to + // either go through `cueVideoById` or `playerVars.start`, but at the time of writing + // both end up resetting the video to the state as if the user hasn't interacted with it. player.seekTo(this.startSeconds, true); }