@@ -71,12 +71,13 @@ export const DEFAULT_PLAYER_HEIGHT = 390;
71
71
// The native YT.Player doesn't expose the set videoId, but we need it for
72
72
// convenience.
73
73
interface Player extends YT . Player {
74
- videoId ?: string | undefined ;
74
+ videoId ?: string ;
75
+ playerVars ?: YT . PlayerVars ;
75
76
}
76
77
77
78
// The player isn't fully initialized when it's constructed.
78
79
// The only field available is destroy and addEventListener.
79
- type UninitializedPlayer = Pick < Player , 'videoId' | 'destroy' | 'addEventListener' > ;
80
+ type UninitializedPlayer = Pick < Player , 'videoId' | 'playerVars' | ' destroy' | 'addEventListener' > ;
80
81
81
82
/**
82
83
* Object used to store the state of the player if the
@@ -157,6 +158,17 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
157
158
}
158
159
private _suggestedQuality = new BehaviorSubject < YT . SuggestedVideoQuality | undefined > ( undefined ) ;
159
160
161
+ /**
162
+ * Extra parameters used to configure the player. See:
163
+ * https://developers.google.com/youtube/player_parameters.html?playerVersion=HTML5#Parameters
164
+ */
165
+ @Input ( )
166
+ get playerVars ( ) : YT . PlayerVars | undefined { return this . _playerVars . value ; }
167
+ set playerVars ( playerVars : YT . PlayerVars | undefined ) {
168
+ this . _playerVars . next ( playerVars ) ;
169
+ }
170
+ private _playerVars = new BehaviorSubject < YT . PlayerVars | undefined > ( undefined ) ;
171
+
160
172
/**
161
173
* Whether the iframe will attempt to load regardless of the status of the api on the
162
174
* page. Set this to true if you don't want the `onYouTubeIframeAPIReady` field to be
@@ -225,6 +237,7 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
225
237
iframeApiAvailableObs ,
226
238
this . _width ,
227
239
this . _height ,
240
+ this . _playerVars ,
228
241
this . _ngZone
229
242
) . pipe ( tap ( player => {
230
243
// Emit this before the `waitUntilReady` call so that we can bind to
@@ -290,6 +303,7 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
290
303
this . _endSeconds . complete ( ) ;
291
304
this . _suggestedQuality . complete ( ) ;
292
305
this . _youtubeContainer . complete ( ) ;
306
+ this . _playerVars . complete ( ) ;
293
307
this . _destroyed . next ( ) ;
294
308
this . _destroyed . complete ( ) ;
295
309
}
@@ -614,15 +628,18 @@ function createPlayerObservable(
614
628
iframeApiAvailableObs : Observable < boolean > ,
615
629
widthObs : Observable < number > ,
616
630
heightObs : Observable < number > ,
631
+ playerVarsObs : Observable < YT . PlayerVars | undefined > ,
617
632
ngZone : NgZone
618
633
) : Observable < UninitializedPlayer | undefined > {
619
634
620
- const playerOptions =
621
- videoIdObs
622
- . pipe (
623
- withLatestFrom ( combineLatest ( [ widthObs , heightObs ] ) ) ,
624
- map ( ( [ videoId , [ width , height ] ] ) => videoId ? ( { videoId, width, height} ) : undefined ) ,
625
- ) ;
635
+ const playerOptions = combineLatest ( [ videoIdObs , playerVarsObs ] ) . pipe (
636
+ withLatestFrom ( combineLatest ( [ widthObs , heightObs ] ) ) ,
637
+ map ( ( [ constructorOptions , sizeOptions ] ) => {
638
+ const [ videoId , playerVars ] = constructorOptions ;
639
+ const [ width , height ] = sizeOptions ;
640
+ return videoId ? ( { videoId, playerVars, width, height } ) : undefined ;
641
+ } ) ,
642
+ ) ;
626
643
627
644
return combineLatest ( [ youtubeContainer , playerOptions , of ( ngZone ) ] )
628
645
. pipe (
@@ -644,22 +661,25 @@ function syncPlayerState(
644
661
player : UninitializedPlayer | undefined ,
645
662
[ container , videoOptions , ngZone ] : [ HTMLElement , YT . PlayerOptions | undefined , NgZone ] ,
646
663
) : UninitializedPlayer | undefined {
647
- if ( ! videoOptions ) {
664
+ if ( player && videoOptions && player . playerVars !== videoOptions . playerVars ) {
665
+ // The player needs to be recreated if the playerVars are different.
666
+ player . destroy ( ) ;
667
+ } else if ( ! videoOptions ) {
648
668
if ( player ) {
669
+ // Destroy the player if the videoId was removed.
649
670
player . destroy ( ) ;
650
671
}
651
672
return ;
652
- }
653
- if ( player ) {
673
+ } else if ( player ) {
654
674
return player ;
655
675
}
656
676
657
677
// Important! We need to create the Player object outside of the `NgZone`, because it kicks
658
678
// off a 250ms setInterval which will continually trigger change detection if we don't.
659
679
const newPlayer : UninitializedPlayer =
660
680
ngZone . runOutsideAngular ( ( ) => new YT . Player ( container , videoOptions ) ) ;
661
- // Bind videoId for future use.
662
681
newPlayer . videoId = videoOptions . videoId ;
682
+ newPlayer . playerVars = videoOptions . playerVars ;
663
683
return newPlayer ;
664
684
}
665
685
0 commit comments