如何使用 javascript API 和 json 获取 youtube 播放列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6560311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a youtube playlist using javascript API and json
提问by plamen
This is part of my youtube project. I try to extract video information from JSON format but I have problem in this line:
这是我的 youtube 项目的一部分。我尝试从 JSON 格式中提取视频信息,但在这一行中遇到了问题:
var videoId = data.feed.entry[i].link[1].href;
When I do this in single line not in cikle evrithing is ok but in cikle for or while I have error.
当我在单行中执行此操作时,不是在 cikle evrithing 中执行此操作,而是在 cikle 中执行此操作时或在出现错误时。
//get youtube ID
function extract(url){
pos1=url.indexOf("videos/");
pos2=url.substr(42,11);
return pos2;
}
//My playlist LINK
var url2="http://gdata.youtube.com/feeds/api/playlists/B2A4E1367126848D?v=2&alt=json";
function playlistinfo(url1) {
$.ajax({
url: url1,
dataType: "jsonp",
success: function (data) { parseresults(data); }
});
}
//whit this get playlist data
function parseresults(data) {
//return playlist clip number
var klipove= data.feed.openSearch$totalResults.$t;
//put clips in <li>
for(i=0;i<=klipove-1;i++) {
var videoId = data.feed.entry[i].link[1].href;
//get video id ID
var id= extract(videoId);
thumb = data.feed.entry[i].media$group.media$thumbnail[0].url;
$('<li><img src="'+thumb+'" alt="'+id+'" class="thumb"/></li>').appendTo('.cont');
}
}
回答by naveen
IMHO, you code can be much shortened if you use $.getJSON
, $.each
Try this.
恕我直言,如果你使用,你的代码可以大大缩短$.getJSON
,$.each
试试这个。
var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/B2A4E1367126848D?v=2&alt=json&callback=?';
var videoURL= 'http://www.youtube.com/watch?v=';
$.getJSON(playListURL, function(data) {
var list_data="";
$.each(data.feed.entry, function(i, item) {
var feedTitle = item.title.$t;
var feedURL = item.link[1].href;
var fragments = feedURL.split("/");
var videoID = fragments[fragments.length - 2];
var url = videoURL + videoID;
var thumb = "http://img.youtube.com/vi/"+ videoID +"/default.jpg";
if (videoID !='videos') {
list_data += '<li><a href="'+ url +'" title="'+ feedTitle +'"><img alt="'+ feedTitle+'" src="'+ thumb +'"</a></li>';
}
});
$(list_data).appendTo(".cont");
});
Demo: Fiddle for the playlist you have provided
演示:为您提供的播放列表小提琴
P.S: Keep in mind that thumbnail for a youtube video could be found at
PS:请记住,可以在以下位置找到 YouTube 视频的缩略图
http://img.youtube.com/vi/{video-id}/default.jpg
(更多信息在这里)
回答by ckng
I found that these lines:
我发现这些行:
var feedURL = item.link[1].href;
var fragments = feedURL.split("/");
var videoID = fragments[fragments.length - 2];
are expecting item.link[1].href to be in this format:
期望 item.link[1].href 采用以下格式:
http://gdata.youtube.com/feeds/api/videos/NAs5it-xjnQ/responses?v=2
However, it does not necessarily work as sometimes item.link[1] gives an URL like
但是,它不一定有效,因为有时 item.link[1] 会给出类似的 URL
http://www.youtube.com/watch?v=Vcp7xz6dfWE&feature=youtube_gdata
Fragments[fragments.length - 2] will end up being "www.youtube.com" instead of the video ID.
Fragments[fragments.length - 2] 最终将成为“www.youtube.com”而不是视频 ID。
I modified it to retrieve the link from item.content.src which always has a fixed format in the URL e.g.
我修改它以从 item.content.src 中检索链接,该链接在 URL 中始终具有固定格式,例如
http://www.youtube.com/v/NAs5it-xjnQ?version=3&f=playlists&app=youtube_gdata
So the final code snippet is something like:
所以最终的代码片段是这样的:
var tmp = item.content.src.split("/").reverse()[0];
var videoID = tmp.substring(0, tmp.indexOf("?"));
which so far has not failed me yet.
到目前为止还没有让我失望。
Hope this helps those who are stuck or is having problems retrieving the video ID.
希望这可以帮助那些被卡住或在检索视频 ID 时遇到问题的人。
Regards
CK
问候
CK
回答by chrisallick
Simplified the solution and got rid of the string manipulation stuff.
简化了解决方案并摆脱了字符串操作的东西。
var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/B2A4E1367126848D?v=2&alt=json&callback=?';
var videoURL= 'http://www.youtube.com/watch?v=';
var vids = new Array();
$.getJSON(playListURL, function(data) {
$.each(data.feed.entry, function(i, item) {
vids.push( item["media$group"]["yt$videoid"]["$t"] );
});
console.log( vids );
});