特定のユーザーのお気に入りに登録している動画リストを取得し、YouTube動画のプレーヤーでそれらの動画を切り替え再生します。
<!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();
},function(){
if(ytplayer) ytplayer.pauseVideo();
});
/* 停止 */
$("#btn_stop").click(function(){
if(ytplayer) ytplayer.stopVideo();
});
/* 動画切り替え */
$("#favlist li a").live("click",function(){
var href=$(this).attr("href");
var hash=href.replace("#","");
ytplayer.cueVideoById(hash);
});
$.getJSON(
"http://gdata.youtube.com/feeds/api/users/cocoism3/favorites",
{
alt:"json"
},
function(xml) {
var feed = xml.feed;
$.each(feed.entry, function(i,item){
var tmp=item.id.$t.split("videos/");
var vid=tmp[tmp.length-1];
var s='';
s+='<a href="#'+vid+'"><img src="'+item.media$group.media$thumbnail[0].url+'" width="60" height="45" align="left" />'+item.title.$t+'</a>';
$("#favlist").append('<li class="video cf">'+s+'</li>');
$(".video").fadeIn(1000);
if(i>=5) continue;
});
}
);
});
/* プレーヤーの準備ができると呼ばれる関数 */
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
ytplayer.cueVideoById("9YEEl52u8XE");
}
</script>
<!-- CSS -->
<style type="text/css">
img { vertical-align:middle; }
#demo {
width:800px;
overflow:hidden;
clear:both;
}
#cleft {
width:560px;
float:left;
}
#cright {
width:230px;
float:right;
}
#cright li:hover {
background:#ffffcc;
}
#cright li img {
margin:0 5px 0 0;
padding:0;
}
</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動画のプレーヤーでそれらの動画を切り替え再生します。</p>
<!-- CODE -->
<div id="demo">
<div id="cleft">
<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><!-- #ytapiplayer -->
<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>
</p>
</div><!-- #cleft -->
<div id="cright">
<ul id="favlist"></ul>
</div><!-- #cright -->
<div><!-- #demo -->
<br clear="all" />
<!-- / CODE -->
</body>
</html>