使用 Youtube API 和 Angular 6 (Typescript)

Using Youtube API with Angular 6 (Typescript)

我能够让 Youtube Iframe API(以某种方式)在我的 Angular 6 应用程序中工作。但是,我想从我控制的列表中为它提供动态 videoIds。我的计划是收听来自播放器的事件,然后在播放完当前视频后加载另一个视频。但我似乎无法解决这个问题。我仍在学习 Typescript 和 Angular,我很好奇为什么我从 IFrame API 播放器中调用的任何函数调用的任何方法都会出现未捕获的类型错误:

这是我的代码:

import { Component, OnInit, AfterViewInit } from '@angular/core';
import { Video } from '../shared/model/video.model';
import { Subscription } from 'rxjs';
import { VideoPlayerService } from '../shared/services/video-player.service';

@Component({
  selector: 'ntv-video-player',
  templateUrl: './video-player.component.html',
  styleUrls: ['./video-player.component.css']
})
export class VideoPlayerComponent implements OnInit, AfterViewInit {

  player;
  video: Video;
  videos: Video[];
  videoSubscription: Subscription;

  constructor(private videoService: VideoPlayerService) { }

  ngAfterViewInit() {
    const doc = (<any>window).document;
    const playerApiScript = doc.createElement('script');
    playerApiScript.type = 'text/javascript';
    playerApiScript.src = 'https://www.youtube.com/iframe_api';
    doc.body.appendChild(playerApiScript);
  }

  ngOnInit() {

    (<any>window).onYouTubeIframeAPIReady = () => {
      this.player = new (<any>window).YT.Player('player', {
        height: '100%',
        width: '100%',
        // videoId: this.getVideo(),
        events: {
          'onReady': this.onPlayerReady,
          'onStateChange': this.onPlayerStateChange
        },
        playerVars: {
          autoplay: 1,
          controls: 0,
          modestbranding: 1,
          // playlist: 'UG3sfZKtCQI,ALZHF5UqnU4,x9ZkC3OgI78',
          rel: 0,
          showInfo: 0
        }
      });
    };
  }



  // The API calls this function when the player's state changes.
  onPlayerStateChange(event) {
    console.log('onPlayerStateChange');
    console.log(event.data);
  }

  // The API will call this function when the video player is ready
  onPlayerReady(event) {
    console.log(event);

    const videoId = this.getVideo();
    event.target.cueVideoById({
      'videoId': videoId
    });
    event.target.playVideo();
  }

  getVideo() {
    return '60ItHLz5WEA';
  }

}

这是我遇到的错误:

    Uncaught TypeError: Cannot read property 'getVideo' of undefined
    at push../src/app/video-player/video-player.component.ts.VideoPlayerComponent.onPlayerReady (video-player.component.ts:63)
    at M.h.G (www-widgetapi.js:48)
    at Y.h.o (www-widgetapi.js:92)
    at Y.h.H (www-widgetapi.js:105)
    at Wa.g (www-widgetapi.js:81)
    at Oa.f (www-widgetapi.js:70)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
    at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:496)
    at invokeTask (zone.js:1540)
push../src/app/video-player/video-player.component.ts.VideoPlayerComponent.onPlayerReady    @   video-player.component.ts:63
h.G @   www-widgetapi.js:48
h.o @   www-widgetapi.js:92
h.H @   www-widgetapi.js:105
Wa.g    @   www-widgetapi.js:81
Oa.f    @   www-widgetapi.js:70
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask    @   zone.js:421
push../node_modules/zone.js/dist/zone.js.Zone.runTask   @   zone.js:188
push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask    @   zone.js:496
invokeTask  @   zone.js:1540
globalZoneAwareCallback

this.getVideo() 如果我从 'videoId' 属性 调用它似乎可以工作,但我希望能够调用它 onPlayerReady 或 onPlayerStateChange 这样我就可以让它更有活力。以前有人做过吗? Angular.

的文档不多

提前致谢。

我认为您遇到的是范围问题,您的 OnPlayerReady 函数未在您的 class 范围内执行,而是在 youtube 播放器的范围内执行。我是从错误消息 "Cannot read property 'getVideo' of undefined" 中推断出来的。在这个执行点 "this" 不是它看起来的样子。

尝试像这样更改您的事件分配代码:

 events: {
          'onReady': (event) => { this.onPlayerReady(event); },
          'onStateChange': (event) => { this.onPlayerStateChange(event); }
        },

谢谢乔尔!这帮助我解决了这个问题。一开始我确实遇到了错误,因为我没有传递参数。所以我这样做了并且成功了!

events: { 'onReady': (event) => { this.onPlayerReady(event); }, 
'onStateChange': (event) => { this.onPlayerStateChange(event); } 
},

我想我很难理解 API 的文档并尝试将其翻译成 Typescript。我只希望他们能在那里添加更多文档。还做这样的事情 (<any>window).onYouTubeIframeAPIReady = () => { 对我来说感觉有点老套。有更好的方法吗?

抱歉,我还没有足够的代表点数,因此无法对您的回答投赞成票。