您好,我正在开发一个android应用程序,我的应用程序的一部分想分析歌曲标题youtube并获得视频链接。获得100%正确的视频无关紧要。所以我如何从YouTube上检索数据?Android从YouTube获取视频链接
任何人都可以帮助我找到一个解决方案,它确实对我有帮助。
感谢
您好,我正在开发一个android应用程序,我的应用程序的一部分想分析歌曲标题youtube并获得视频链接。获得100%正确的视频无关紧要。所以我如何从YouTube上检索数据?Android从YouTube获取视频链接
任何人都可以帮助我找到一个解决方案,它确实对我有帮助。
感谢
最常见的方法是使用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
它支持搜索以下基本参数:
使用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());
}
嗨感谢所有你们指着我在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
上述请求“title_you_want_to_search”获得更多的信息就是你要搜索的关键词。我们可以通过将额外的参数传递给url来自定义结果。
首先,我们需要从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可能会抛出异常。 可能有更好的方法来做到这一点。如果有任何建议
不再可用 – Hatim
我已经更新了答案,以Youtube搜索的新API调用 –