Skip to content

Commit 883466e

Browse files
authored
fix(youtube-player): error when interacting with the player before the API has been loaded (angular#29127)
Fixes a couple of issues: 1. We were using the `PlayerState` enum before the API has been loaded which will cause a runtime error because the enum is usually read off the `window`. 2. Fixes that calling the `playVideo` method wasn't load the YouTube API. Fixes angular#29030.
1 parent 263dadf commit 883466e

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/youtube-player/youtube-player.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const DEFAULT_PLAYER_HEIGHT = 390;
7878
* user tries to interact with the API before it has been loaded.
7979
*/
8080
interface PendingPlayerState {
81-
playbackState?: YT.PlayerState.PLAYING | YT.PlayerState.PAUSED | YT.PlayerState.CUED;
81+
playbackState?: PlayerState.PLAYING | PlayerState.PAUSED | PlayerState.CUED;
8282
playbackRate?: number;
8383
volume?: number;
8484
muted?: boolean;
@@ -90,6 +90,19 @@ function coerceTime(value: number | undefined): number | undefined {
9090
return value == null ? value : numberAttribute(value, 0);
9191
}
9292

93+
/**
94+
* Equivalent of `YT.PlayerState` which we can't use, because it's meant to
95+
* be read off the `window` which we can't do before the API has been loaded.
96+
*/
97+
enum PlayerState {
98+
UNSTARTED = -1,
99+
ENDED = 0,
100+
PLAYING = 1,
101+
PAUSED = 2,
102+
BUFFERING = 3,
103+
CUED = 5,
104+
}
105+
93106
/**
94107
* Angular component that renders a YouTube player via the YouTube player
95108
* iframe API.
@@ -280,7 +293,8 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy {
280293
if (this._player) {
281294
this._player.playVideo();
282295
} else {
283-
this._getPendingState().playbackState = YT.PlayerState.PLAYING;
296+
this._getPendingState().playbackState = PlayerState.PLAYING;
297+
this._load(true);
284298
}
285299
}
286300

@@ -289,7 +303,7 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy {
289303
if (this._player) {
290304
this._player.pauseVideo();
291305
} else {
292-
this._getPendingState().playbackState = YT.PlayerState.PAUSED;
306+
this._getPendingState().playbackState = PlayerState.PAUSED;
293307
}
294308
}
295309

@@ -299,7 +313,7 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy {
299313
this._player.stopVideo();
300314
} else {
301315
// It seems like YouTube sets the player to CUED when it's stopped.
302-
this._getPendingState().playbackState = YT.PlayerState.CUED;
316+
this._getPendingState().playbackState = PlayerState.CUED;
303317
}
304318
}
305319

@@ -411,7 +425,7 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy {
411425
return this._pendingPlayerState.playbackState;
412426
}
413427

414-
return YT.PlayerState.UNSTARTED;
428+
return PlayerState.UNSTARTED;
415429
}
416430

417431
/** See https://developers.google.com/youtube/iframe_api_reference#getCurrentTime */
@@ -581,7 +595,7 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy {
581595
// Only cue the player when it either hasn't started yet or it's cued,
582596
// otherwise cuing it can interrupt a player with autoplay enabled.
583597
const state = player.getPlayerState();
584-
if (state === YT.PlayerState.UNSTARTED || state === YT.PlayerState.CUED || state == null) {
598+
if (state === PlayerState.UNSTARTED || state === PlayerState.CUED || state == null) {
585599
this._cuePlayer();
586600
}
587601

@@ -598,13 +612,13 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy {
598612
const {playbackState, playbackRate, volume, muted, seek} = pendingState;
599613

600614
switch (playbackState) {
601-
case YT.PlayerState.PLAYING:
615+
case PlayerState.PLAYING:
602616
player.playVideo();
603617
break;
604-
case YT.PlayerState.PAUSED:
618+
case PlayerState.PAUSED:
605619
player.pauseVideo();
606620
break;
607-
case YT.PlayerState.CUED:
621+
case PlayerState.CUED:
608622
player.stopVideo();
609623
break;
610624
}

0 commit comments

Comments
 (0)