有没有办法用Javascript从Youtube频道中只提取最新的视频?
我有个想法 使用Youtube feed,但我真的不知道如何使用javascript实现它。
如果有任何实际的例子,我将非常感激。
经过今天上午的努力搜索,我现在得到它的工作完美。
$(function() {
var API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var PLAYLIST_ID = 'PLDvUaV28ulciewonYGknOdIw8lAJfXIzn';
var GOOGLE_API_URL = 'https://www.googleapis.com/youtube/v3/playlistItems?part=id%2C+snippet%2C+contentDetails&playlistId='+PLAYLIST_ID+ '&key=' + API_KEY + '&callback=showVideos';
$.ajax({
url: GOOGLE_API_URL,
dataType: 'jsonp',
crossDomain: true
});
window.showVideos = function(data) {
if (data.items && data.items.length > 0) {
$("#video").html('<iframe width="504" height="283" src="http://www.youtube.com/embed/'+data.items[0].contentDetails.videoId+'" frameborder="0" allowfullscreen></iframe>');
}
}
});
使用jQuery。
$.getJSON('https://gdata.youtube.com/feeds/api/videos?author=yourchannel&orderby=published', function(data) {
//data represents JSON Object of videos
}
如果我设置的参数正确,数据应该代表所有的视频按发布时间排序,所以最后一个是最新的。