我可以获取直播列表:https://developers.google.com/youtube/v3/live/docs/liveStreams/list#examples,但是如何在android中使用youtube v3 api获取特定频道的直播流状态和直播流链接或id?
发布于 2017-07-04 00:58:44
我也遇到过同样的问题,并设法找到了同样的解决方案。YouTube实际上为此目的提供了一个应用程序接口。如果您有频道ID,您可以使用搜索API获取当前活动的Live Video ID。
使用以下API:
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={YOUR_CHANNEL_ID}&eventType=live&type=video&key={YOUR_API_KEY}这里的关键因素是您必须将eventType设置为live,将type设置为video。
为了获取频道ID,如果您有频道的用户名,可以使用以下请求。
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={CHANNEL_USER_NAME}&key={YOUR_API_KEY}发布于 2017-07-04 16:04:46
返回直播流ID
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={CHANNEL_ID}&eventType=live&type=video&key={YOUR_API_KEY}您还可以将查询传递给YouTube,以便YouTube为您提供所有实况流事件
https://www.googleapis.com/youtube/v3/search?part=snippet&q={YOUR_SEARCH_QuERY}&eventType=live&type=video&key={YOUR_YOUTUBE_API}这就是我如何在我的应用程序中获取实时流视频
private void initVolley(String[] urls) {
for (String url : urls) {
if (url.startsWith("https://www.googleapis.com/youtube/v3/")) {
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("items");
if (jsonArray.length() > 0) {
for (int getItem = 0; getItem < jsonArray.length(); getItem++) {
JSONObject jsonObject = jsonArray.getJSONObject(getItem).getJSONObject("id");
String videoID = jsonObject.getString("videoId");
Log.d(TAG, "void id " + videoID);
mUrlList.add(new video(jsonArray.getJSONObject(getItem).getJSONObject("snippet").getString("title"), jsonArray.getJSONObject(getItem).getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("medium").getString("url"), " ", "https://www.youtube.com/watch?v=" + videoID, true));
if (mAdapter != null)
mAdapter.notifyDataSetChanged();
Log.d(TAG, videoID + jsonArray.length() + jsonArray.getJSONObject(getItem).getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("default").getString("url"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);
}
}
}如果你有YouTube链接,也许你把你的存储在数据库中,那么这将返回一些有用的信息,而不是API键
https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=lUOhCtXPN40&format=json下面是这个的代码
JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.GET, JsonURL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// mVideo = new video(response.getString("title"), response.getString("thumbnail_url"), response.getString("author_name"));
// mVideoDataSet.add(mVideo);
mVideo = new video(url);
mVideo.setVideoTitle(response.getString("title"));
mVideo.setVideoThimailUrl(response.getString("thumbnail_url"));
mVideo.setVideoChannalName(response.getString("author_name"));
mVideo.setLiveNow(false);
mVideoDataSet.add( mVideo);
Log.d(TAG, "inside the volly" + mVideo.getVideoTitle());
// Log.d(TAG, mVideoDataSet.get(i).getVideoStreemUrl());
if (mAdapter != null) {
mAdapter.notifyDataSetChanged();
// if(mSwipeRefreshLayout.isRefreshing())
// mSwipeRefreshLayout.setRefreshing(false);
} else {
Log.d(TAG, "setingAdupter");
// if(mSwipeRefreshLayout.isRefreshing())
// mSwipeRefreshLayout.setRefreshing(false);
mAdapter = new CustomAdapter(mVideoDataSet);
mRecyclerView.setAdapter(mAdapter);
}
i++;
if (i != mUrlList.size())
initVolly();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// if(error)
i++;
initVolly();
Log.e("Volley", "Error");
}
}
);
// Adds the JSON object request "obreq" to the request queue
requestQueue.add(obreq);最后,如果你想在这里看到源代码,is link
https://stackoverflow.com/questions/37521853
复制相似问题