Problem Statement
1)I am a beginner at implementing youtube api and I want to retrieve VideoMetaData such as duration of video and stream url.
MyAnalysis :
1)I have used the youtube api to retrieve this metaData such as Video Tilte,VideoID and creation Date.
2)The problem is I am not able to see other data such as Video duration and stream url in the Response.
//Also,StartTime and EndTime has been depreciated.So I cannot find duration
Then,how can I fetch these details related to video.
Below is the response I am getting.
Code for the Same :
public class Search1 {
private static YouTube.PlaylistItems.List playlistItemRequest;
private static String PLAYLIST_ID = "PL8WtjSOdakGuBu0q9EKLXndc4jcAQxs2Y";
private static final Long NUMBER_OF_VIDEOS_RETURNED = (long) 10;
private static String apiKey="AIzaSyDJAaPLW5wbWdfKX6CjfvSo5yrF3K3rlwc";
private static YouTube youtube;
public static void main(String s[]){
youtube = new YouTube.Builder(new NetHttpTransport(),
new JacksonFactory(), new HttpRequestInitializer()
{
@Override
public void initialize(HttpRequest hr) throws IOException {}
}).setApplicationName("youtube-cmdline-search-sample").build();
List<PlaylistItem> playlistItemList = new ArrayList<PlaylistItem>();
try
{
playlistItemRequest = youtube.playlistItems().list("snippet,contentDetails");
playlistItemRequest.setPlaylistId(PLAYLIST_ID);
playlistItemRequest.setFields("items(snippet/title,snippet/playlistId,snippet/publishedAt,snippet/description,snippet/thumbnails/default/url,contentDetails/videoId,contentDetails/startAt,contentDetails/endAt,contentDetails/videoPublishedAt),nextPageToken,pageInfo");
playlistItemRequest.setKey(apiKey);
// videoItem.setId(item.getContentDetails().getVideoId());
String nextToken = "";
do {
playlistItemRequest.setPageToken(nextToken);
PlaylistItemListResponse playlistItemResult = playlistItemRequest.execute();
playlistItemList.addAll(playlistItemResult.getItems());
nextToken = playlistItemResult.getNextPageToken();
} while (nextToken != null);
}catch(IOException e)
{
System.out.println("Exception"+e);
}
Iterator iteratorSearchResults=playlistItemList.iterator();
while (iteratorSearchResults.hasNext()) {
PlaylistItem playlist = (PlaylistItem) iteratorSearchResults.next();
// String duration=(playlist.getContentDetails().getStartAt()-playlist.getContentDetails().getEndAt());
System.out.println(" Title: " + playlist.getSnippet().getTitle());
System.out.println(" Video Created Date" + playlist.getContentDetails().getVideoPublishedAt());
System.out.println(" PlayList ID: " + playlist.getSnippet().getPlaylistId());
System.out.println(" Video ID: " + playlist.getContentDetails().getVideoId());
System.out.println(" Stream url of PlayList: ");
System.out.println(" Start Time: " + playlist.getContentDetails().getStartAt());
System.out.println(" End Time: " + playlist.getContentDetails().getEndAt());
System.out.println(" Duration " );
}
}
}