【发布时间】:2017-04-27 22:06:44
【问题描述】:
我正在使用 YouTube Iframe,如下所示。我想让我的播放器在 API 加载后通过单击按钮加载。
我创建了一个系统,我网站的用户可以选择将哪些视频加载到播放列表中,因此我无法对 YouTube 视频 ID 进行硬编码。是否可以创建一个 onclick 函数来触发加载播放器的事件?如果有,怎么做??
非常感谢!!!
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: { //CAN I REPLACE THE ONREADY EVENT WITH ONE
'onReady': onPlayerReady, //THAT FIRES ONCLICK?
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
【问题讨论】:
标签: javascript jquery html youtube youtube-api