の定義を延期していますonYouTubePlayerAPIReady
。このため、YouTube APIonYouTubePlayerAPIReady
が定義される前に終了する可能性があります。その場合、コードは失敗します。
この問題を解決するには、実行時に 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()
やなどの単純な一方向関数について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");
}