【问题标题】:How to get duration of YouTube video via YouTubeDataApi v3 in Android?如何在 Android 中通过 YouTubeDataApi v3 获取 YouTube 视频的时长?
【发布时间】:2020-11-21 11:15:42
【问题描述】:

我正在使用 YouTubeData Api v3 从搜索结果中获取视频的 ID、标题、描述和缩略图。现在我还想获取视频的持续时间,这是我无法使用以下代码实现的。

如何获取视频的时长?我也设置了部分和字段,如代码所示以接收属性。

private int youTubeConnector() {
    try {
        youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
            @Override
            public void initialize(HttpRequest request) {
            }
        }).setApplicationName(getResources().getString(R.string.app_name)).build();

    } catch(ExceptionInInitializerError e){
        e.printStackTrace();
        return -1;
    } catch(RuntimeException e){
        e.printStackTrace();
        return -1;
    } catch(Exception e){
        e.printStackTrace();
        return -1;
    }

    try {
        query = youtube.search().list("id,snippet,contentDetails");
        query.setKey(Constants.YOUTUBE);
        query.setType("video");
        query.setFields("items(id/kind,id/videoId,snippet/title,snippet/description,snippet/thumbnails/high/url,contentDetails/duration),nextPageToken,prevPageToken,pageInfo");

        return 1;
    } catch (IOException e) {
        e.printStackTrace();
        return -1;
    } catch (Exception e){
        e.printStackTrace();
        return -1;
    }
}

public List<YouTubeVideoItem> search(String keywords) {
    query.setQ(keywords);
    query.setMaxResults(YOUTUBE_MAXRESULTS);

    if(nextPage == true) {
        query.setPageToken(nextPageToken);
        nextPage = false;
    }
    else if(previousPage == true) {
        query.setPageToken(previousPageToken);
        previousPage = false;
    }

    try {
        SearchListResponse response = query.execute();
        nextPageToken = response.getNextPageToken();
        previousPageToken = response.getPrevPageToken();

        List<SearchResult> results = response.getItems();
        List<YouTubeVideoItem> items = new ArrayList<YouTubeVideoItem>();

        if(results != null) {
            items = setItemsList(results.iterator());
        }
        return items;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (Exception e){
        e.printStackTrace();
        return null;
    }
}

private List<YouTubeVideoItem> setItemsList(Iterator<SearchResult> iteratorSearchResults) {
    List<YouTubeVideoItem> tempSetItems = new ArrayList<>();

    while(iteratorSearchResults.hasNext()) {
        SearchResult singleVideo = iteratorSearchResults.next();
        ResourceId rId = singleVideo.getId();

        try{
            if (rId.getKind().equals("youtube#video")) {
                YouTubeVideoItem item = new YouTubeVideoItem();
                Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().getHigh();

                item.setId(singleVideo.getId().getVideoId());
                item.setTitle(singleVideo.getSnippet().getTitle());
                item.setDescription(singleVideo.getSnippet().getDescription());
                item.setThumbnailURL(thumbnail.getUrl());


                tempSetItems.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    return tempSetItems;
}

【问题讨论】:

    标签: android json youtube youtube-data-api google-apis-explorer


    【解决方案1】:

    根据the docsSearch.list API 端点不提供它包含在返回给调用者的结果集中的任何视频项目的持续时间属性。

    要访问您感兴趣的任何视频的duration 属性,您必须使用适当查询的Videos.list 端点。

    请注意,后一个端点接受其id 参数作为以逗号分隔的视频 ID 列表。因此,对于一组N 视频ID,您不必向Videos.list 发出N 调用,但是,如果N &lt;= 50,只有一个这样的调用具有正确分配的id 参数。

    【讨论】:

      猜你喜欢
      • 2015-11-30
      • 2018-06-28
      • 1970-01-01
      • 2017-05-26
      • 2019-09-12
      • 2015-12-07
      • 2016-10-16
      • 1970-01-01
      • 2021-03-13
      相关资源
      最近更新 更多