gpt4 book ai didi

angular - 将 Youtube API 与 Angular 6 (Typescript) 结合使用

转载 作者:太空狗 更新时间:2023-10-29 18:22:41 26 4
gpt4 key购买 nike

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

这是我的代码:

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

如果我从“videoId”属性调用 this.getVideo() 似乎可以正常工作,但我希望能够调用它 onPlayerReady 或 onPlayerStateChange 以便使其更具动态性。以前有人做过吗? Angular 的文档不多。

提前致谢。

最佳答案

我认为您遇到的是范围问题,您的 OnPlayerReady 函数未在您的类范围内执行,而是在 youtube 播放器的范围内执行。我是从错误消息“无法读取未定义的属性‘getVideo’”中推断出来的。在这个执行点,“this”并不是它看起来的样子。

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

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

关于angular - 将 Youtube API 与 Angular 6 (Typescript) 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52268100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com