Skip to content

Commit 7832c3d

Browse files
authored
Fix null issue in url youtube url when running a playlist with no videoId
When playing a playlist via playerVars.list = [playlistId] we dont have a videoId available yet. Plus we don't need one, Youtube will start at the first video of the playlist by default. By injecting an undefined videoId in the YT Player constructur, we end up with null value in the iframe url (`ps://www.youtube.com/embed/null?cc_load_policy=1&enablejsapi=1&controls=1&showinfo=0&rel=0&listType=playlist&list=PLxf07yK_HAvdwhW-L0X3uP5aMe6eEt50v`) which is causing an Invalid Video Id error for consumers.
1 parent 15e9a57 commit 7832c3d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/youtube-player/youtube-player.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -587,17 +587,21 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy {
587587

588588
// Important! We need to create the Player object outside of the `NgZone`, because it kicks
589589
// off a 250ms setInterval which will continually trigger change detection if we don't.
590+
const params: any = {
591+
host: this.disableCookies ? 'https://www.youtube-nocookie.com' : undefined,
592+
width: this.width,
593+
height: this.height,
594+
// Calling `playVideo` on load doesn't appear to actually play
595+
// the video so we need to trigger it through `playerVars` instead.
596+
playerVars: playVideo ? {...(this.playerVars || {}), autoplay: 1} : this.playerVars,
597+
}
598+
// We only want to injecct a videoId if one is provided, otherwise loading a playlist via playerVars.list, the missing videoId will create a null value in the youtube iframe url and that can trigger a JS error `Invalid video id` in widget api.
599+
if (this.videoId) {
600+
params.videoId = this.videoId;
601+
}
590602
const player = this._ngZone.runOutsideAngular(
591603
() =>
592-
new YT.Player(this.youtubeContainer.nativeElement, {
593-
videoId: this.videoId,
594-
host: this.disableCookies ? 'https://www.youtube-nocookie.com' : undefined,
595-
width: this.width,
596-
height: this.height,
597-
// Calling `playVideo` on load doesn't appear to actually play
598-
// the video so we need to trigger it through `playerVars` instead.
599-
playerVars: playVideo ? {...(this.playerVars || {}), autoplay: 1} : this.playerVars,
600-
}),
604+
new YT.Player(this.youtubeContainer.nativeElement, params),
601605
);
602606

603607
const whenReady = (event: YT.PlayerEvent) => {

0 commit comments

Comments
 (0)