ngOnInit()

in src/youtube-player/youtube-player.ts [216:295]


  ngOnInit() {
    // Don't do anything if we're not in a browser environment.
    if (!this._isBrowser) {
      return;
    }

    let iframeApiAvailableObs: Observable<boolean> = observableOf(true);
    if (!window.YT || !window.YT.Player) {
      if (this.showBeforeIframeApiLoads && (typeof ngDevMode === 'undefined' || ngDevMode)) {
        throw new Error(
          'Namespace YT not found, cannot construct embedded youtube player. ' +
            'Please install the YouTube Player API Reference for iframe Embeds: ' +
            'https://developers.google.com/youtube/iframe_api_reference',
        );
      }

      const iframeApiAvailableSubject = new Subject<boolean>();
      this._existingApiReadyCallback = window.onYouTubeIframeAPIReady;

      window.onYouTubeIframeAPIReady = () => {
        if (this._existingApiReadyCallback) {
          this._existingApiReadyCallback();
        }
        this._ngZone.run(() => iframeApiAvailableSubject.next(true));
      };
      iframeApiAvailableObs = iframeApiAvailableSubject.pipe(take(1), startWith(false));
    }

    // An observable of the currently loaded player.
    const playerObs = createPlayerObservable(
      this._youtubeContainer,
      this._videoId,
      iframeApiAvailableObs,
      this._width,
      this._height,
      this._playerVars,
      this._ngZone,
    ).pipe(
      tap(player => {
        // Emit this before the `waitUntilReady` call so that we can bind to
        // events that happen as the player is being initialized (e.g. `onReady`).
        this._playerChanges.next(player);
      }),
      waitUntilReady(player => {
        // Destroy the player if loading was aborted so that we don't end up leaking memory.
        if (!playerIsReady(player)) {
          player.destroy();
        }
      }),
      takeUntil(this._destroyed),
      publish(),
    );

    // Set up side effects to bind inputs to the player.
    playerObs.subscribe(player => {
      this._player = player;

      if (player && this._pendingPlayerState) {
        this._initializePlayer(player, this._pendingPlayerState);
      }

      this._pendingPlayerState = undefined;
    });

    bindSizeToPlayer(playerObs, this._width, this._height);

    bindSuggestedQualityToPlayer(playerObs, this._suggestedQuality);

    bindCueVideoCall(
      playerObs,
      this._videoId,
      this._startSeconds,
      this._endSeconds,
      this._suggestedQuality,
      this._destroyed,
    );

    // After all of the subscriptions are set up, connect the observable.
    (playerObs as ConnectableObservable<Player>).connect();
  }