我试图使用他们的API包含YouTube视频。然而,有时(真随机大多数时候它的工作原理),当我点击我的播放按钮,我得到一个错误:Youtube API有时还没有准备好
Cannot read property 'playVideo' of undefined
这是我包括API:
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('video-intro', {
width: 100,
height: 100,
videoId: '<id>',
playerVars: {
color: 'white',
controls: 0,
showinfo: 0
}
});
}
$(document).ready(function() {
$('#play-intro').click(function() {
player.playVideo();
$('#video-intro').addClass('showvid');
});
});
#video-intro {
display: none;
&.showvid {
display: block;
}
}
<head>
<script src="https://www.youtube.com/iframe_api"></script>
</head>
<body>
<div id="video-intro"></div>
<a id="play-intro">Play</a>
</body>
嗯,不过现在我的视频开始在后台播放已经(我听到这个声音,持续6秒钟)。我只希望它按下按钮时播放。另外,我的“关闭/停止”按钮不再有效。我的关闭按钮调用这些函数:player.pauseVideo(); player.seekTo(0);.但它给出了停止视频和pauseVideo不存在的错误。 – Cake
你的意思是你想播放器播放/暂停按钮,或者你需要外部按钮来播放? @Cake –
检查这个例子http://jsfiddle.net/BeUFb/326/ @Cake –