2012-08-28 151 views
4

您好,我正在開發一個android應用程序,我的應用程序的一部分想分析歌曲標題youtube並獲得視頻鏈接。獲得100%正確的視頻無關緊要。所以我如何從YouTube上檢索數據?Android從YouTube獲取視頻鏈接

任何人都可以幫助我找到一個解決方案,它確實對我有幫助。

感謝

回答

3

最常見的方法是使用Youtube數據API,它將返回XML/Json,您可以解析它以檢索視頻網址等內容。

更新(2017年1月24日)(V3)

使用以下調用使用搜索查詢來搜索YouTube視頻:

https://www.googleapis.com/youtube/v3/search?part=snippet&q=fun%20video&key=YOUR-API-KEY 

它支持搜索以下基本參數:

  • part:您想要在搜索中檢索的視頻數據。對於基本搜索,推薦值爲片段
  • q:您的谷歌開發者API密鑰:你要搜索
  • 關鍵的文本。該密鑰可以在應用程序的Credentials頁面的Google Developer API Console處獲得。請確保在您的密鑰所屬的應用程序上啓用Youtube Data API v3。

詳細參數見Google API Documentation

使用Java庫

在Android上,你可以使用的平臺上提供標準的HTTP請求類做一個HTTP請求的URL,或您可以使用Google API Java Library如下圖所示:

 YouTube youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() { 
      public void initialize(HttpRequest request) throws IOException { 
      } 
     }).setApplicationName("YOUR-APPLICATION-NAME").build(); 

     String queryTerm = "A fun video" 

     // Define the API request for retrieving search results. 
     YouTube.Search.List search = youtube.search().list("id,snippet"); 

     search.setKey("Your-Api-Key"); 
     search.setQ(queryTerm); 

     // Call the API and print first result. 
     SearchListResponse searchResponse = search.execute(); 
     if(searchResponse.getItems().size() == 0) 
     { 
      //No items found. 
      return; 
     } 
     SearchResult firstItem = searchResponse.getItems().get(0); 

     ResourceId rId = firstItem.getId(); 
     // Confirm that the result represents a video. Otherwise, the 
     // item will not contain a video ID. 
     if (rId.getKind().equals("youtube#video")) { 
      Thumbnail thumbnail = firstItem.getSnippet().getThumbnails().getDefault(); 

      Log.d("YOUTUBE_SAMPLE","Video Id" + rId.getVideoId()); 
      Log.d("YOUTUBE_SAMPLE","Title: " + firstItem.getSnippet().getTitle()); 
      Log.d("YOUTUBE_SAMPLE","Thumbnail: " + thumbnail.getUrl()); 
     } 
+0

不再可用 – Hatim

+0

我已經更新了答案,以Youtube搜索的新API調用 –

1

嗨感謝所有你們指着我在W唉,我想休閒。我終於想出了一些東西,也想分享我的期滿

根據youtube,我們可以請求數據爲xml或json。我用JSON的方法對我實施

http://gdata.youtube.com/feeds/api/videos?q=title_you_want_to_search&max-results=1&v=2&alt=jsonc

你可以從youtube developer guide

上述請求「title_you_want_to_search」獲得更多的信息就是你要搜索的關鍵詞。我們可以通過將額外的參數傳遞給url來自定義結果。

  • 「最大結果」:別說你要多少成績獲得(在我的情況 我只是想只有一個)
  • 「ALT」:你想要的格式的結果JSON或XML

首先,我們需要從Youtube API中請求數據,然後我們必須選擇我們想從數組中選擇哪一部分信息。在我的情況下,我用「數據」和「項目」來獲取視頻。我們固定該視頻ID後,那麼我們就可以使視頻URL這樣

String mVideoLink = "https://youtu.be/"+videoID; (我用的以下功能讓這件事做)

public String readYoutubeFeed(String songTitle) { 
StringBuilder builder = new StringBuilder(); 
HttpClient client = new DefaultHttpClient(); 
String url = "http://gdata.youtube.com/feeds/api/videos?q="+songTitle+"&max-results=1&v=2&alt=jsonc"; 
try { 
    URLEncoder.encode(url, "UTF-8"); 
} catch (UnsupportedEncodingException e1) { 
    e1.printStackTrace(); 
    Log.v(TAG,"encode error"); 
    } 
HttpGet httpGet = new HttpGet(url);   
    try { 
     HttpResponse response = client.execute(httpGet); 
     StatusLine statusLine = response.getStatusLine(); 
     int statusCode = statusLine.getStatusCode(); 
     if (statusCode == 200) { 
     HttpEntity entity = response.getEntity(); 
     InputStream content = entity.getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(content, "UTF-8")); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      builder.append(line); 
     } 
     } else { 
     Log.v(TAG,"Failed to download file"); 
     } 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
     Log.v(TAG,"readYoutubeFeed exeption1"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.v(TAG,"readYoutubeFeed exeption2"); 
    } 
    return builder.toString(); 
    } 

public String getYouTubeVideoId(String songTitle){ 
String jesonData = readYoutubeFeed(songTitle); 
Log.i(TAG,jesonData); 
String title = "123";   
try {  
    SONObject jObj = new JSONObject(jesonData); 
    JSONArray ja = jObj.getJSONObject("data").getJSONArray("items"); 
    JSONObject jo = (JSONObject) ja.get(0); 
    title = jo.getString("id");    
    Log.v(TAG,"id is " +title); 

} catch (Exception e) { 
    e.printStackTrace(); 
    Log.v(TAG,"error occerd"); 
    } 
return title; 

}

一個重要的事情要提這將字符串轉換爲「UTF-8」是想做的,因爲創建JsonArray可能會拋出異常。 可能有更好的方法來做到這一點。如果有任何建議

+0

只想說謝謝,您的代碼片段爲我提供了很好的參考。 – Jeremy

+0

它不再可用的api – Hatim