2017-07-06 139 views
0

我已经制作了YouTube视频的布局。我想设置视频的缩略图和标题。 (我成功地设置缩略图而不是标题)如何通过YouTube API获取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()。

回答

0

要获得我不喜欢这个影片名称:

public static String getVideoTitle(String youtubeVideoUrl) { 
    try { 
     if (youtubeVideoUrl != null) { 
      URL embededURL = new URL("http://www.youtube.com/oembed?url=" + 
       youtubeVideoUrl + "&format=json" 
      ); 

      return new JSONObject(IOUtils.toString(embededURL)).getString("title"); 
     } 

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

参考是: Get title of YouTube video

+0

是的,这是我写的,我发现的解决方案。奇怪的是,我找不到来自YouTube API的东西 –

+0

让我看看json –

+0

哪个json?我不知道我知道如何得到它 –

1
从YouTube

JSON数据是:

{... 
"items": [ 
    { 
    "kind": "youtube#video", 
    "etag": ".....", 
    "id": "....", 
    "snippet": { 
    "publishedAt": ".....", 
    "channelId": "...", 
    "title": "This is the title", 
    "description": "", 
    "thumbnails": { 
    "default": { 
     "url": "https://....jpg", 
     "width": 120, 
     "height": 90 
.... 

拿到冠军:

JsonArray items = jsondata.getAsJsonArray("items"); 
JsonObject snippet = item.getAsJsonObject("snippet"); 
String title = snippet.get("title").getAsString(); 

我建议使用https://github.com/koush/ion加载数据。

0
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); 


} 
相关问题