gpt4 book ai didi

java - 如何通过YouTube API获取YouTube视频的标题

转载 作者:行者123 更新时间:2023-12-01 17:55:49 25 4
gpt4 key购买 nike

我有 YouTube 视频的布局。我想设置视频的缩略图和标题。 (我成功设置了缩略图但没有设置标题)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1.0">

<com.google.android.youtube.player.YouTubeThumbnailView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="6dp"
android:layout_marginTop="10dp"
android:id="@+id/youtubeThumbnailView"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YouTube Video"
android:textSize="20sp"
android:layout_marginLeft="4dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:id="@+id/textViewTitle"/>
</LinearLayout>

我在这里设置缩略图:

YouTubeThumbnailView youTubeThumbnailView = (YouTubeThumbnailView) newChild.findViewById(R.id.youtubeThumbnailView);
youTubeThumbnailView.initialize(YouTubePlayer.DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) {
youTubeThumbnailLoader.setVideo("FM7MFYoylVs");
youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
youTubeThumbnailLoader.release();
}

@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
}
});
}

@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
}
});

如何从视频中获取标题?

我在这里看到了一个解决方案:Get title of YouTube video

但我不确定这是否是正确的方法。我想 YouTube API 可以让我们以更简单的方式获取标题,例如:youTubeThumbnailView.getText()。

最佳答案

private void getVideoInfo(){

// volley
StringRequest stringRequest = new StringRequest(
Request.Method.GET,
"https://www.googleapis.com/youtube/v3/videos?id=" + keyYouTubeVideo + "&key=" +
API_KEY +
"&part=snippet,contentDetails,statistics,status",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {

try {

JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("items");

JSONObject object = jsonArray.getJSONObject(0);
JSONObject snippet = object.getJSONObject("snippet");

String title = snippet.getString("title");


Log.d("stuff: ", "" + title);


} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.activity_main, error.getMessage(), Toast.LENGTH_LONG).show();
}
}){};

// Request (if not using Singleton [RequestHandler]
// RequestQueue requestQueue = Volley.newRequestQueue(this);
// requestQueue.add(stringRequest);

// Request with RequestHandler (Singleton: if created)
RequestHandler.getInstance(MainActivity.activity_main).addToRequestQueue(stringRequest);


}

关于java - 如何通过YouTube API获取YouTube视频的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44941341/

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