应用程序在本地主机上运行良好,在本地主机上可以播放所有视频,当部署到服务器上时,大多数示例视频都无法工作。
由于这些视频既可以在本地主机上播放,也可以在YouTube网站上播放,所以我不确定这里缺少什么,在dev工具上也看不到任何警告。
例如,这里是示例视频id:vlkNcHDFnGA,我还可以看到视频可以直接在iframe上播放:
https://jsfiddle.net/skjagini/419wcuod/
YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "key",
ApplicationName = this.GetType().ToString()
});
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = searchText;
searchListRequest.MaxResults = 5; // 50
searchListRequest.Type = "video";
searchListRequest.VideoSyndicated = SearchResource.ListRequest.VideoSyndicatedEnum.True__;
searchListRequest.VideoEmbeddable = SearchResource.ListRequest.VideoEmbeddableEnum.True__;
在.html文件中
<section class="app-footer footer">
<div class="container-fluid">
<span class="float-left">
<div class="player-defaults" id="video-player"></div>
</span>
在.ts文件中
this.player = YouTubePlayer('video-player', {
height: 60, width: 100
});
this.player.on('stateChange', (event) => {
if (!this.stateNames[event.data]) {
throw new Error('Unknown state (' + event.data + ').');
// console.log()
} else if (event.data === 0) {
this.playNext();
}
});
playVideoById(vidoeId: string) {
if (vidoeId == null) {
if (this.activeSong) {
vidoeId = this.activeSong.videoId;
} else {
return;
}
}
this.isPlaying = true;
// 'loadVideoById' is queued until the player is ready to receive API calls.
this.player.loadVideoById(vidoeId); // player.loadVideoById('M7lc1UVf-VE');
this.playVideo();
}
playVideo() {
this.isPlaying = true;
// 'playVideo' is queue until the player is ready to received API calls and after 'loadVideoById' has been called.
this.player.playVideo();
}
编辑:当我右击youtube播放器并选择embedded video时,它似乎生成了正确的html,这与我的JSFiddle相同
<iframe width="1920" height="1080" src="https://www.youtube.com/embed/vlkNcHDFnGA" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
如果我点击youtube的标志,它会在youtube浏览器中打开,播放视频就很好了。
编辑:
我注意到我得到的是CORD(不是CORS错误),因为我的服务器是http,而youtube在幕后调用的google ads服务是https。这可能是个问题吗?