您可以使用此:
class YouTubeVideoInfo {
private String channel;
private String url;
private long views;
private int comments;
private int ratings;
private int likes;
private int dislikes;
private String thumbnail;
private String title;
.....
}
public static final String YOUTUBE_GDATA_SERVER = "http://gdata.youtube.com";
public static final String USER_FEED_PREFIX = YOUTUBE_GDATA_SERVER + "/feeds/api/users/";
public static final String UPLOADS_FEED_SUFFIX = "/uploads";
...............
public YouTubeVideoInfo getVideoInfo(YouTubeService service, String channel, String url) {
VideoFeed videoFeed = service.getFeed(
new URL(USER_FEED_PREFIX + channel + UPLOADS_FEED_SUFFIX), VideoFeed.class);
List<VideoEntry> videoEntries = videoFeed.getEntries();
for (VideoEntry videoEntry : videoEntries) {
YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();
if (mediaGroup != null && mediaGroup.getPlayer() != null && videoEntry.getTitle() != null) {
if (url.equals(mediaGroup.getPlayer().getUrl())) {
String title = videoEntry.getTitle().getPlainText();
MediaKeywords keywords = mediaGroup.getKeywords();
MediaPlayer mediaPlayer = mediaGroup.getPlayer();
final YtStatistics statistics = videoEntry.getStatistics();
final YouTubeVideoInfo videoInfo = new YouTubeVideoInfo(channel,
mediaPlayer.getUrl(), statistics != null
? statistics.getViewCount() : 0);
if (videoEntry.getComments() != null
&& videoEntry.getComments().getFeedLink() != null)
videoInfo.comments =
videoEntry.getComments().getFeedLink().getCountHint();
final Rating rating = videoEntry.getRating();
if (rating != null)
videoInfo.ratings = rating.getNumRaters();
final YtRating ytRating = videoEntry.getYtRating();
if (ytRating != null) {
videoInfo.likes = ytRating.getNumLikes();
videoInfo.dislikes = ytRating.getNumDislikes();
}
final List<MediaThumbnail> thumbnails = mediaGroup.getThumbnails();
if (!thumbnails.isEmpty())
videoInfo.thumbnail = thumbnails.get(thumbnails.size()/2).getUrl();
if (videoEntry.getTitle() != null)
videoInfo.title = videoEntry.getTitle().getPlainText();
return videoInfo;
}
}
.... // exception handling
謝謝回答! 我對Java相當陌生,所以無法弄清楚你的代碼的哪部分要使用,哪些部分要編輯。一些幫助將非常感激.. – chet 2012-03-27 21:21:43
對於第一類添加setters/getters(通常IDE生成它們)。將常量添加到方法所在的另一個類中。只需添加異常處理或聲明拋出它們的方法即可。這幾乎是完整的代碼,不需要很多工作就可以使用。 我相信你已經找到了如何獲得在YouTube上驗證的YouTubeService實例,對吧? – 2012-03-27 21:29:04
我不認爲我需要身份驗證,因爲我沒有執行任何寫入,更新或刪除操作。我對麼?另一方面(Java),讓我再嘗試一些,如果我仍然不知道如何使其工作,我會回來。再次感謝! :) – chet 2012-03-27 21:44:28