PHP & JavaScript Room :: 設置サンプル

%E8%A8%AD%E7%BD%AE%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB

実行結果

設置サンプル

YouTube IFrame API:動画プレーヤーの制御(再生・一時停止・停止・シーク位置)

再生 停止 |  00:30から再生

設置サンプルのソース

<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8" />
      <title>設置サンプル:YouTube IFrame API:動画プレーヤ―の制御(再生・一時停止・停止・シーク位置)</title>
      <link rel="stylesheet" type="text/css" href="/content/lib/global.css" />
      <style>
         #playerbox { width:640px; height:390px; margin-bottom:10px; }
         #controls  { width:640px; text-align:center; }
         #controls a { display:inline-block; margin:0; padding:5px 10px; text-decoration:none; width:6em; color:#fff; background:#000; text-align:center; }
         #controls a:hover { background:#333; }
         #controls a#seek { width:10em; }
      </style>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
      <script>
         $(function(){
            var player;
            var videoID="XfkXW49bXfo";
            /* IFrame Player APIのコードをロード */
            function fGetScript(){
               $.ajax({
                  url:"http://www.youtube.com/player_api/",
                  dataType:"script",
                  success:function(data){
                     dbg("done");
                  },
                  error:function(xhr, status, thrown) {
                     dbg(xhr);
                     fGetScript();
                  }
               }); 
            }
            fGetScript();
            /* プレーヤーの準備完了時 */
            window.onYouTubeIframeAPIReady=function() {
               dbg("onYouTubeIframeAPIReady");
               loadPlayer(videoID);
            }
            /* プレーヤー生成 */
            function loadPlayer(videoID) {
               dbg("loadPlayer("+videoID+")");
               if(!player){
                  player = new YT.Player(
                     'player',{
                        width: '640',   /* 動画プレーヤーの幅 */
                        height: '390',   /* 動画プレーヤーの高さ */
                        videoId: videoID,   /* YouTube動画ID */
                        events: { /* イベント */
                           "onReady": onPlayerReady,   /* プレーヤの準備完了時 */
                           "onStateChange": onPlayerStateChange,   /* 動画プレーヤーのステータス変更 */
                        },
                        playerVars: {
                           "rel":0,         // 関連動画の有無(default:1)
                           "showinfo":0,      // 動画情報表示(default:1)
                           "controls":1      // コントロール有無(default:1)
                        }
                     }
                  );
               }else{
                  player.loadVideoById(videoID);
               }
            }
            function onPlayerReady(event){
               dbg("onPlayerReady");
               /* プレーヤの準備完了時 */
            }
            function onPlayerStateChange(event) {
               /* プレーヤーのステータスが変更される度に発生 */
               dbg("PlayerState:"+event.data);
               switch(event.data){
                  case YT.PlayerState.ENDED:
                  case YT.PlayerState.PAUSED:
                  case YT.PlayerState.CUED:
                     $("#play").html("再生");
                     break;
                  case YT.PlayerState.PLAYING:
                  case YT.PlayerState.BUFFERING:
                     $("#play").html("一時停止");
                     break;
                  default:
                     $("#play").html("再生");
                     break;
               }
            }
            function play(){
               dbg("play");
               player.playVideo(); /* 再生 */
               $(this).html("一時停止");
            }
            function pause(){
               dbg("pause");
               player.pauseVideo(); /* 一時停止 */
               $(this).html("再生");
            }
            function stop(){
               dbg("stop");
               player.stopVideo(); /* 停止 */
               player.cueVideoById(videoID);
               $(this).html("一時停止");
            }
            $("#play").click(function(){
               var label=$(this).html();
               dbg("play");
               if(label=="再生"){
                  play();
               }else{
                  pause();
               }
            });
            $("#stop").click(function(){
               stop();
            });
            $("#seek").click(function(){
               player.seekTo(30,true);
               play();
            });
            function dbg(str){
               $("#debuglog").val(str+"\n"+$("#debuglog").val());
                  if(window.console && window.console.log){
                    console.log(str);
                  }
            }
         });
      </script>
   <link rel="stylesheet" type="text/css" href="/common/css/example.css"></head>
   <body id='example3' class='example'><div class="ads" style="margin:32px auto;text-align:center;"></div><h1 class='h'><a href='/'>PHP &amp; JavaScript Room</a> :: 設置サンプル</h1>
<h2 class='h'>%E8%A8%AD%E7%BD%AE%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB</h2>
<h3 class='h'>実行結果</h3>
      <div id="wrap">
         <h1>設置サンプル</h1>
         <h2>YouTube IFrame API:動画プレーヤーの制御(再生・一時停止・停止・シーク位置)</h2>
         <div id="playerbox"><div id='player'><!-- 動画プレーヤーの埋め込み --></div></div>
         <p id="controls">
            <a href="#" id="play">再生</a>
            <a href="#" id="stop">停止</a> | 
            <a href="#" id="seek">00:30から再生</a>
         </p>
         <textarea id="debuglog" style="width:630px;height:300px;"></textarea>
      </div>
   </body>
</html>