问题
How can I get a list of videos available from a particular YouTube channel, using the API?
回答1:
Basically every youtube channels has three section: Uploads, Playlist, Liked Videos. Had done some work with the playlist of a channels. Used youtube api version 3.Sharing my code:
First get the Playlists from a channel:
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
}
}
Then get the playListItem or Videos:
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
}
}
Hadn't worked with the other two(Uploads,Liked videos), but think it can be possible in a similar manner.
来源:https://stackoverflow.com/questions/20795665/how-can-i-get-a-list-of-videos-available-from-a-particular-youtube-channel