我怎样才能获得的视频列表可以从一个特定的YouTube频道?(How can I get a lis

2019-09-29 14:36发布

我怎样才能获得的视频列表可以从一个特定的YouTube频道,使用API​​?

Answer 1:

基本上每个YouTube频道有三个部分:上传,播放列表,喜欢的影片。 已经做了一些工作,一个频道的播放列表。 透过YouTube API版本3.Sharing我的代码:

首先从一个频道的播放清单:

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
    }
}

然后拿到playListItem或视频:

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
    }
}

没有与其他两个工作(上传,喜欢的影片),但认为它可以以类似的方式是可行的。



文章来源: How can I get a list of videos available from a particular YouTube channel?