YouTube JavaScript APIを使用して、現在の動画の長さ、URL、埋め込みコードを取得するサンプル。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="ja" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="imagetoolbar" content="no" /> <title>設置サンプル</title> <link rel="stylesheet" type="text/css" href="/content/lib/global.css" /> <!-- JS --> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script> <script type="text/javascript"> var flashvars = { }; var params = { allowScriptAccess:"always" }; var attributes = { id:"myytplayer" }; swfobject.embedSWF( "http://www.youtube.com/apiplayer?enablejsapi=1&version=3&playerapiid=ytplayer", "ytapiplayer", "560", "340", "8.0.0", /* Flash Player8 以降を指定 */ "/content/lib/swfobject/expressInstall.swf", flashvars, params, attributes ); $(function(){ /* 再生/一時停止 */ $("#btn_play").toggle(function(){ if(ytplayer) ytplayer.playVideo(); return false; },function(){ if(ytplayer) ytplayer.pauseVideo(); return false; }); /* 停止 */ $("#btn_stop").click(function(){ if(ytplayer) ytplayer.stopVideo(); return false; }); }); /* プレーヤーの準備ができると呼ばれる関数 */ function onYouTubePlayerReady(playerId) { ytplayer = document.getElementById("myytplayer"); ytplayer.cueVideoById("9YEEl52u8XE"); timerID=setInterval("getStatus()",1); var s=""; s+="動画のURL:<br><input type='text' style='width:560px;border:1px solid #ccc;' value='"+ytplayer.getVideoUrl()+"' /><br>"; s+="動画の埋め込みコード:<br><textarea style='width:560px;height:150px;border:1px solid #ccc;background:#eee;'>"+ytplayer.getVideoEmbedCode()+"</textarea>"; $("#status").append(s); } function getStatus(){ var s=""; s+="再生中の動画の長さ:"+ytplayer.getDuration()+"<br>"; $("#duration").html(s); } </script> <!-- CSS --> <style type="text/css"> img { vertical-align:middle; } </style> <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 & JavaScript Room</a> :: 設置サンプル</h1> <h3 class='h'>実行結果</h3> <h1>設置サンプル</h1> <h2>FlashのWeb埋め込みライブラリ「SWFObject」を使用したYouTube動画プレーヤーの埋め込み(クロムレスプレーヤー)</h2> <p>YouTube JavaScript APIを使用して、現在の動画の長さ、URL、埋め込みコードを取得するサンプル。</p> <!-- CODE --> <div id="ytapiplayer"> <p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p> </div> <p> <a href="#" id="btn_play"><img src="/content/img/strm/key_play_pause.png" alt="再生/一時停止" /></a> <a href="#" id="btn_stop"><img src="/content/img/strm/key_stop.png" alt="停止" /></a> <span id="duration"></span> </p> <div id="status"></div> <!-- / CODE --> </body> </html>