hal*_*uud 1 javascript youtube jquery youtube-api
我有这个测试滑块http://jsfiddle.net/dUyLY/2/
在Chrome中,它运行良好,但在Firefox和Safari中它会在动画完成后出错.在脚本文件中,我检查每个动画后,如果当前可见元素是youtube播放器,如果是,则播放视频.当离开幻灯片暂停时.
我在控制台中收到此错误 player.pauseVideo is not a function
有人有解决方案吗?
你推迟了定义onYouTubePlayerAPIReady
.出于这个原因,YouTube API可能在之前完成onYouTubePlayerAPIReady
定义.在这种情况下,您的代码将失败.
要解决此问题,请检查API是否在运行时准备就绪.
window.onYouTubePlayerAPIReady = function () {
player = new YT.Player('player', {
height: '315',
width: '560',
videoId: 'bpOR_HuHRNs',
});
};
if (window.YT) {
// Apparently, the API was ready before this script was executed.
// Manually invoke the function
onYouTubePlayerAPIReady();
}
Run Code Online (Sandbox Code Playgroud)
注意.对于简单的单向函数,例如player.playVideo()
和player.pauseVideo()
,我建议使用这个简单的函数,它不像文档化的YouTube API那样臃肿.看到这个答案.
以下是您网页的更新部分,使用callPlayer
我的其他答案而非YouTube API中的功能:http://jsfiddle.net/ryyCZ/
<div id="player">
<iframe width="560" height="315" frameborder="0" src="http://www.youtube.com/embed/bpOR_HuHRNs?enablejsapi=1"></iframe>
</div>
if ($(".slides_control > div:visible #player").length == 1) {
callPlayer("player","playVideo");
} else {
callPlayer("player", "pauseVideo");
}
Run Code Online (Sandbox Code Playgroud)