我使用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
回调中获取该信息?
答案 0 :(得分:0)
您无法从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");