1

我有这个测试滑块http://jsfiddle.net/dUyLY/2/

在 Chrome 中它运行良好,但在 Firefox 和 safari 中,当动画完成时它会出错。在脚本文件中,我在每个动画之后检查当前可见元素是否是 youtube 播放器,如果是则播放视频。离开幻灯片时暂停它。

我在控制台中收到此错误player.pauseVideo is not a function

有人对此有解决方案吗?

4

1 回答 1

5

您正在推迟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();
}

笔记。对于简单的单向函数,比如player.playVideo()and 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");
}
于 2012-07-02T13:00:03.760 回答