cakePHPのviewでYouTubeAPIを初めて使った。divのid="videoDiv"内に動画が自動で再生される。閲覧中断していた動画は中断したところから再生される。閲覧中断ボタンと、閲覧完了ボタンに対応している。といったようなことをした。
<?php $this->start('script')?> <script src="//www.google.com/jsapi" type="text/javascript"></script> <script> var ytplayer = null; <?php if($watch):?> var seek = <?php echo h($watch['Watch']['seek'])?>; <?php else:?> var seek = 0; <?php endif;?> google.load("swfobject", "2.1"); function _run() { // The video to load. var videoID = "<?php echo h($movie['Movie']['code'])?>"; // Lets Flash from another domain call JavaScript var params = { allowScriptAccess: "always" }; // The element id of the Flash embed var atts = { id: "ytPlayer" }; // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/) swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "?enablejsapi=1&playerapiid=player1", "videoDiv", "560", "315", "9", null, null, params, atts); } google.setOnLoadCallback(_run); function onYouTubePlayerReady(playerId) { ytplayer = document.getElementById("ytPlayer"); ytplayer.seekTo(seek, true); ytplayer.playVideo(); } //動画閲覧の中断 function stop(movie_id){ if(ytplayer){ var seek = ytplayer.getCurrentTime(); if(seek > 0){ location.href = '<?php echo $this->Html->url(array('controller' => 'movies', 'action' => 'stop'))?>' + '/' + movie_id + '/' + seek; } } } //動画閲覧の終了 function finish(movie_id){ location.href = '<?php echo $this->Html->url(array('controller' => 'movies', 'action' => 'finish'))?>' + '/' + movie_id; } </script> <?php $this->end()?>