gpt4 book ai didi

android - 使用YouTubeThumbnailView获取视频信息

转载 作者:行者123 更新时间:2023-12-03 06:19:12 25 4
gpt4 key购买 nike

我正在使用YouTubeThumbnailView显示链接到我的应用的视频的缩略图。

我正在使用该代码:

youtube_thumb.initialize(getContext().getString(R.string.google_api_key), new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {

youTubeThumbnailLoader.setVideo(url);
youtube_thumb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (YouTubeIntents.canResolvePlayVideoIntentWithOptions(getContext())) {
//Opens in the YouTube app in fullscreen and returns to this app once the video finishes
getContext().startActivity(YouTubeIntents.createPlayVideoIntentWithOptions(getContext(), url, true, true));
}else{
getContext().startActivity(YouTubeStandalonePlayer.createVideoIntent((AppCompatActivity) getContext(), getContext().getString(R.string.google_api_key), url));
}
}
});
}

@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
youtube_thumb.setVisibility(GONE);
}
});

我现在需要显示视频信息,例如标题和描述。

有没有办法在 onInitializationSuccess回调中获取该信息?

最佳答案

您无法从YouTubeThumbnailView获取信息,但是可以使用YouTube数据API,并使用okhttp做类似的事情:
(检查它是否有效,这是我使用youtube api时的旧代码)

StringBuilder builder = new StringBuilder("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=");
builder.append(url)
.append("&fields=items/snippet/title,items/snippet/description&key=")
.append("API_KEY");
Request request = new Request.Builder().url(builder.toString()).build();
Response response = null;
response = client.newCall(request).execute();
if (!response.isSuccessful()) {
//DO SOMETHING
}
JSONObject snippet=new JSONObject(response.body().string())
.getJSONArray("items").getJSONObject(0).getJSONObject("snippet");
String title=snippet.getString("title");
String description=snippet.getString("description");

关于android - 使用YouTubeThumbnailView获取视频信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38514971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com