我在构建播放器时遵循了Youtube API。我需要通过javascript调用autoplay函数,但它不会监听setTimeout函数,并且它不会开始播放:
有人可以点亮我错在哪里吗?
问候!
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: {
'onReady': onPlayerReady,
}
});
}
function onPlayerReady(event){
setTimeout(function(){
playVideo();
},5000);
}
答案 0 :(得分:-1)
当您声明player
变量并稍后将其设置为YT.Player
的实例时,您必须在setTimeout
上使用相同的变量。
setTimeout(function(){
player.playVideo();
},5000);