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); }