我正在使用 YouTube API 使用 Javascript 和 Jquery 从 YouTube 播放列表中检索视频。并将视频显示到 Carousal 中。有时该视频会加载到 carousal 中,有时则不是。当我替换播放列表 ID 时它工作正常,但一段时间后我又遇到了类似的问题。
<script type="text/javascript">
function fnGoogleFeedLoaded(){
fnCreateCarousal(1,"2C5A4567D2B2600B");
fnCreateCarousal(2,"9bw4S5ePsEFtdohdvpRVwqPvPorrFTQF");
fnCreateCarousal(3,"PL9A7F57201F1AA96B");
fnCreateCarousal(4,"88CBC02990158C01");
fnCreateCarousal(5,"PL0155DF7A9B0DF819");
fnCreateCarousal(6,"3367D45095D0B592");
}
function fnCreateCarousal(nIndex,nPlaylistID){
var feedUrl = "http://gdata.youtube.com/feeds/api/playlists/"+nPlaylistID+"?max-results=8";
new google.feeds.lookupFeed(feedUrl, function (result) {
if (result.error || !result.url) {
$('#videocomm').hide();
return;
}
// get the feed items
var feed = new google.feeds.Feed(result.url);
feed.setNumEntries(50);
feed.load(function (result) {
// write out the feed data
var container = $("#CarousalBase_"+nIndex);
//var totalcount = result.feed.entries.length;
//alert (totalcount);
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
var vidhash = /=(.*)&(.*)$/.exec (entry.link)[1];
/* container.append('<div><a href="http://www.youtube.com/embed/'+vidhash+'" class="html5lightbox" title="'+entry.title+'"><img src="http://img.youtube.com/vi/'+vidhash+'/0.jpg" /><br />'+entry.title+'</a></div>\n');*/
container.append('<div><a href="#" onClick=onYouTubeIframeAPIReady("'+vidhash+'"); title="'+entry.title+'"><img src="http://img.youtube.com/vi/'+vidhash+'/0.jpg" /><br />'+entry.title+'</a></div>\n');
}
// load the first video
$(".html5lightbox:first").each(function () {
if(nIndex == 1){
loadVideo(this);
return false;
}
});
// setup the click handler for all the videso
$(".html5lightbox").click(function () {
loadVideo(this);
return false;
});
});
});
}
google.setOnLoadCallback(function () {
//alert("Hii");
fnGoogleFeedLoaded();
});
google.load("feeds", "1");
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
function onYouTubeIframeAPIReady(vidhash)
{
player = new YT.Player('light', {
height: '390',
width: '640',
videoId: vidhash,
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';
}
function onPlayerReady(event) {
event.target.playVideo();
}
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 2000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}