如何使用
API 获取特定 YouTube 频道可用的视频列表?
基本上每个YouTube频道都有三个部分:上传,播放列表,喜欢的视频。对频道的播放列表做了一些工作。使用的 youtube api 版本 3.分享我的代码:
首先从频道获取播放列表:
private void getPlayList() {
YouTube.Playlists.List playLists;
try {
playLists = youtube.playlists().list("id,status,snippet");//youtube is the Youtube object, already initialised
playLists.setChannelId(channelID);//channelID is the channel id which you want to fetch
playLists.setFields("items(id,status/privacyStatus,snippet(title,thumbnails/default/url))");
playLists.setMaxResults((long) 50);
AsynRequestClass asynRequestClass = new AsynRequestClass();
asynRequestClass.execute(playLists);
} catch (IOException e) {
e.printStackTrace();
Log.e(null, "Error occur " + e.toString());
}
}
private class AsynRequestClass extends AsyncTask<YouTube.Playlists.List, Void, PlaylistListResponse> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showProgressDialogWithTitle("Loading PlayList");
}
@Override
protected PlaylistListResponse doInBackground(YouTube.Playlists.List... params) {
PlaylistListResponse playlistListResponse = null;
try {
Log.d(null, "PlayListList: " + params[0]);
playlistListResponse = params[0].execute();
Log.d(null,"PlayListResponse: "+playlistListResponse);
for (int i=0;i<playlistListResponse.getItems().size();i++){
//PlayListIdentifier,PlayListTitle,PlayListThumbnails are ArrayList<String>, already allocated and initialised
PlayListTitles.add(playlistListResponse.getItems().get(i).getSnippet().getTitle());
PlayListThumbnails.add(playlistListResponse.getItems().get(i).getSnippet().getThumbnails().getDefault().getUrl());
PlayListIdentifier.add(playlistListResponse.getItems().get(i).getId());
}
}catch (UserRecoverableAuthIOException e){
startActivityForResult(e.getIntent(),REQUEST_AUTHORIZATION);
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(PlaylistListResponse playlistListResponse){
super.onPostExecute(playlistListResponse);
hideProgressDialog();
PlayListDataAdapter.notifyDataSetChanged();//PlayListDataAdapter is the data adapter
}
}
然后获取播放列表项或视频:
private void loadPlayListItem(){
YouTube.PlaylistItems.List playListItemList = null;
try {
playListItemList =youtube.playlistItems().list("id,contentDetails,snippet,status");
playListItemList.setPlaylistId(playListID);
playListItemList.setFields("items(id,status/privacyStatus,snippet(title,thumbnails/default/url),contentDetails/regionRestriction)");
playListItemList.setMaxResults((long) 50);
AsyncRequestClass asyncRequestClass = new AsyncRequestClass();
asyncRequestClass.execute(playListItemList);
} catch (IOException e) {
e.printStackTrace();
}
}
//Class for asynchronous task
private class AsyncRequestClass extends AsyncTask<YouTube.PlaylistItems.List, Void, PlaylistItemListResponse> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showProgressDialogWithTitle("Loading Video");
}
@Override
protected PlaylistItemListResponse doInBackground(YouTube.PlaylistItems.List... params) {
PlaylistItemListResponse playlistItemListResponse = null;
try {
Log.d(null, "PlayListListItem: " + params[0]);
playlistItemListResponse = params[0].execute();
Log.d(null,"PlayListItemListResponse: "+playlistItemListResponse);
int size = playlistItemListResponse.getItems().size();
for (int i=0;i<size;i++){
if (!playlistItemListResponse.getItems().get(i).getStatus().getPrivacyStatus().equals("private")){
//videoListIdentifier,videoListTitle,videoListThumbnails are ArrayList<String>, already allocated and initialised
videoTitles.add(playlistItemListResponse.getItems().get(i).getSnippet().getTitle());
videoThumbnails.add(playlistItemListResponse.getItems().get(i).getSnippet().getThumbnails().getDefault().getUrl());
videoIdentifier.add(playlistItemListResponse.getItems().get(i).getContentDetails().getVideoId());
}
}
}catch (UserRecoverableAuthIOException e){
startActivityForResult(e.getIntent(),REQUEST_AUTHORIZATION);
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(PlaylistItemListResponse playlistItemListResponse) {
super.onPostExecute(playlistItemListResponse);
hideProgressDialog();
videoListDataAdapter.notifyDataSetChanged();//videoListDataAdapter is the data adapter
}
}
没有与其他两个(上传,喜欢的视频)一起工作,但认为可以以类似的方式实现。