app/src/main/java/com/smedic/tubtub/YouTubeSearch.java
public void acquirePlaylistVideos(final String playlistId) {

        // Define a list to store items in the list of uploaded videos.
        new Thread(new Runnable() {
            @Override
            public void run() {

                Log.d(TAG, "Chosen name: " + mChosenAccountName);
                credential.setSelectedAccountName(mChosenAccountName);

                youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
                        .setApplicationName(appName).build();

                ArrayList<PlaylistItem> playlistItemList = new ArrayList<PlaylistItem>();
                ArrayList<YouTubeVideo> playlistItems = new ArrayList<>();

                String nextToken = "";
                // Retrieve the playlist of the channel's uploaded videos.
                YouTube.PlaylistItems.List playlistItemRequest = null;
                try {
                    playlistItemRequest = youtube.playlistItems().list("id,contentDetails,snippet");
                    playlistItemRequest.setPlaylistId(playlistId);
                    playlistItemRequest.setMaxResults(50l);
                    playlistItemRequest.setFields("items(contentDetails/videoId,snippet/title," +
                            "snippet/thumbnails/default/url),nextPageToken");
                    // Call API one or more times to retrieve all items in the list. As long as API
                    // response returns a nextPageToken, there are still more items to retrieve.
                    //do {
                    //playlistItemRequest.setPageToken(nextToken);
                    PlaylistItemListResponse playlistItemResult = playlistItemRequest.execute();
                    playlistItemList.addAll(playlistItemResult.getItems());
                    //nextToken = playlistItemResult.getNextPageToken();
                    //} while (nextToken != null);

                    Log.d(TAG, "all items size: " + playlistItemList.size());
                } catch (GoogleJsonResponseException e) {
                    if (e.getStatusCode() == 404) {
                        youTubeVideosReceiver.onPlaylistNotFound(playlistId, e.getStatusCode());
                        return;
                    } else {
                        e.printStackTrace();
                    }
                } catch (UnknownHostException e) {
                    Toast.makeText(activity.getApplicationContext(), "Check internet connection", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                    return;
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }

                //videos to get duration
                YouTube.Videos.List videosList = null;
                try {
                    videosList = youtube.videos().list("id,contentDetails");
                    videosList.setKey(Config.YOUTUBE_API_KEY);
                    videosList.setFields("items(contentDetails/duration)");

                    //save all ids from searchList list in order to find video list
                    StringBuilder contentDetails = new StringBuilder();

                    int ii = 0;
                    for (PlaylistItem result : playlistItemList) {
                        contentDetails.append(result.getContentDetails().getVideoId());
                        if (ii < playlistItemList.size() - 1)
                            contentDetails.append(",");
                        ii++;
                    }
                    //find video list
                    videosList.setId(contentDetails.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }

                VideoListResponse resp = null;
                try {
                    resp = videosList.execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                List<Video> videoResults = resp.getItems();
                Iterator<PlaylistItem> pit = playlistItemList.iterator();
                Iterator<Video> vit = videoResults.iterator();
                while (pit.hasNext()) {
                    PlaylistItem playlistItem = pit.next();
                    Video videoItem = vit.next();

                    YouTubeVideo youTubeVideo = new YouTubeVideo();
                    youTubeVideo.setId(playlistItem.getContentDetails().getVideoId());
                    youTubeVideo.setTitle(playlistItem.getSnippet().getTitle());
                    youTubeVideo.setThumbnailURL(playlistItem.getSnippet().getThumbnails().getDefault().getUrl());
                    //video info
                    if (videoItem != null) {
                        String isoTime = videoItem.getContentDetails().getDuration();
                        String time = Utils.convertISO8601DurationToNormalTime(isoTime);
                        youTubeVideo.setDuration(time);
                    } else {
                        youTubeVideo.setDuration("NA");
                    }
                    playlistItems.add(youTubeVideo);
                }

                youTubeVideosReceiver.onVideosReceived(playlistItems);
            }
        }).start();
    }
/**
     * Acquires all videos for a specific playlist
     *
     * @param playlistId
     */
Acquires all videos for a specific playlist
@param playlistId
Acquires all videos for a specific playlist @param playlistId
public void acquirePlaylistVideos(final String playlistId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "Chosen name: " + mChosenAccountName);
                credential.setSelectedAccountName(mChosenAccountName);
                youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
                        .setApplicationName(appName).build();
                ArrayList<PlaylistItem> playlistItemList = new ArrayList<PlaylistItem>();
                ArrayList<YouTubeVideo> playlistItems = new ArrayList<>();
                String nextToken = "";
                YouTube.PlaylistItems.List playlistItemRequest = null;
                try {
                    playlistItemRequest = youtube.playlistItems().list("id,contentDetails,snippet");
                    playlistItemRequest.setPlaylistId(playlistId);
                    playlistItemRequest.setMaxResults(50l);
                    playlistItemRequest.setFields("items(contentDetails/videoId,snippet/title," +
                            "snippet/thumbnails/default/url),nextPageToken");
                    PlaylistItemListResponse playlistItemResult = playlistItemRequest.execute();
                    playlistItemList.addAll(playlistItemResult.getItems());
                    Log.d(TAG, "all items size: " + playlistItemList.size());
                } catch (GoogleJsonResponseException e) {
                    if (e.getStatusCode() == 404) {
                        youTubeVideosReceiver.onPlaylistNotFound(playlistId, e.getStatusCode());
                        return;
                    } else {
                        e.printStackTrace();
                    }
                } catch (UnknownHostException e) {
                    Toast.makeText(activity.getApplicationContext(), "Check internet connection", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                    return;
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
                YouTube.Videos.List videosList = null;
                try {
                    videosList = youtube.videos().list("id,contentDetails");
                    videosList.setKey(Config.YOUTUBE_API_KEY);
                    videosList.setFields("items(contentDetails/duration)");
                    StringBuilder contentDetails = new StringBuilder();
                    int ii = 0;
                    for (PlaylistItem result : playlistItemList) {
                        contentDetails.append(result.getContentDetails().getVideoId());
                        if (ii < playlistItemList.size() - 1)
                            contentDetails.append(",");
                        ii++;
                    }
                    videosList.setId(contentDetails.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                VideoListResponse resp = null;
                try {
                    resp = videosList.execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                List<Video> videoResults = resp.getItems();
                Iterator<PlaylistItem> pit = playlistItemList.iterator();
                Iterator<Video> vit = videoResults.iterator();
                while (pit.hasNext()) {
                    PlaylistItem playlistItem = pit.next();
                    Video videoItem = vit.next();
                    YouTubeVideo youTubeVideo = new YouTubeVideo();
                    youTubeVideo.setId(playlistItem.getContentDetails().getVideoId());
                    youTubeVideo.setTitle(playlistItem.getSnippet().getTitle());
                    youTubeVideo.setThumbnailURL(playlistItem.getSnippet().getThumbnails().getDefault().getUrl());
                    if (videoItem != null) {
                        String isoTime = videoItem.getContentDetails().getDuration();
                        String time = Utils.convertISO8601DurationToNormalTime(isoTime);
                        youTubeVideo.setDuration(time);
                    } else {
                        youTubeVideo.setDuration("NA");
                    }
                    playlistItems.add(youTubeVideo);
                }
                youTubeVideosReceiver.onVideosReceived(playlistItems);
            }
        }).start();
    }
public void acquirePlaylistVideos ( final String playlistId ) { new Thread ( new Runnable ( ) { @ Override public void run ( ) { Log . d ( TAG , "Chosen name: " + mChosenAccountName ) ; credential . setSelectedAccountName ( mChosenAccountName ) ; youtube = new YouTube . Builder ( new NetHttpTransport ( ) , new JacksonFactory ( ) , credential ) . setApplicationName ( appName ) . build ( ) ; ArrayList < PlaylistItem > playlistItemList = new ArrayList < PlaylistItem > ( ) ; ArrayList < YouTubeVideo > playlistItems = new ArrayList < > ( ) ; String nextToken = "" ; YouTube . PlaylistItems . List playlistItemRequest = null ; try { playlistItemRequest = youtube . playlistItems ( ) . list ( "id,contentDetails,snippet" ) ; playlistItemRequest . setPlaylistId ( playlistId ) ; playlistItemRequest . setMaxResults ( 50l ) ; playlistItemRequest . setFields ( "items(contentDetails/videoId,snippet/title," + "snippet/thumbnails/default/url),nextPageToken" ) ; PlaylistItemListResponse playlistItemResult = playlistItemRequest . execute ( ) ; playlistItemList . addAll ( playlistItemResult . getItems ( ) ) ; Log . d ( TAG , "all items size: " + playlistItemList . size ( ) ) ; } catch ( GoogleJsonResponseException e ) { if ( e . getStatusCode ( ) == 404 ) { youTubeVideosReceiver . onPlaylistNotFound ( playlistId , e . getStatusCode ( ) ) ; return ; } else { e . printStackTrace ( ) ; } } catch ( UnknownHostException e ) { Toast . makeText ( activity . getApplicationContext ( ) , "Check internet connection" , Toast . LENGTH_SHORT ) . show ( ) ; e . printStackTrace ( ) ; return ; } catch ( IOException e ) { e . printStackTrace ( ) ; return ; } YouTube . Videos . List videosList = null ; try { videosList = youtube . videos ( ) . list ( "id,contentDetails" ) ; videosList . setKey ( Config . YOUTUBE_API_KEY ) ; videosList . setFields ( "items(contentDetails/duration)" ) ; StringBuilder contentDetails = new StringBuilder ( ) ; int ii = 0 ; for ( PlaylistItem result : playlistItemList ) { contentDetails . append ( result . getContentDetails ( ) . getVideoId ( ) ) ; if ( ii < playlistItemList . size ( ) - 1 ) contentDetails . append ( "," ) ; ii ++ ; } videosList . setId ( contentDetails . toString ( ) ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } VideoListResponse resp = null ; try { resp = videosList . execute ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } List < Video > videoResults = resp . getItems ( ) ; Iterator < PlaylistItem > pit = playlistItemList . iterator ( ) ; Iterator < Video > vit = videoResults . iterator ( ) ; while ( pit . hasNext ( ) ) { PlaylistItem playlistItem = pit . next ( ) ; Video videoItem = vit . next ( ) ; YouTubeVideo youTubeVideo = new YouTubeVideo ( ) ; youTubeVideo . setId ( playlistItem . getContentDetails ( ) . getVideoId ( ) ) ; youTubeVideo . setTitle ( playlistItem . getSnippet ( ) . getTitle ( ) ) ; youTubeVideo . setThumbnailURL ( playlistItem . getSnippet ( ) . getThumbnails ( ) . getDefault ( ) . getUrl ( ) ) ; if ( videoItem != null ) { String isoTime = videoItem . getContentDetails ( ) . getDuration ( ) ; String time = Utils . convertISO8601DurationToNormalTime ( isoTime ) ; youTubeVideo . setDuration ( time ) ; } else { youTubeVideo . setDuration ( "NA" ) ; } playlistItems . add ( youTubeVideo ) ; } youTubeVideosReceiver . onVideosReceived ( playlistItems ) ; } } ) . start ( ) ; }
Acquires all videos for a specific playlist
 @param playlistId
Acquires all videos for a specific playlist @param playlistId
// Define a list to store items in the list of uploaded videos. // Retrieve the playlist of the channel's uploaded videos. // Call API one or more times to retrieve all items in the list. As long as API // response returns a nextPageToken, there are still more items to retrieve. //do { //playlistItemRequest.setPageToken(nextToken); //nextToken = playlistItemResult.getNextPageToken(); //} while (nextToken != null); //videos to get duration //save all ids from searchList list in order to find video list //find video list //video info