首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在安卓系统中通过Youtube API V3的频道Id获取Youtube直播?

如何在安卓系统中通过Youtube API V3的频道Id获取Youtube直播?
EN

Stack Overflow用户
提问于 2016-05-30 17:13:19
回答 2查看 14K关注 0票数 13

我可以获取直播列表:https://developers.google.com/youtube/v3/live/docs/liveStreams/list#examples,但是如何在android中使用youtube v3 api获取特定频道的直播流状态和直播流链接或id?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-04 00:58:44

我也遇到过同样的问题,并设法找到了同样的解决方案。YouTube实际上为此目的提供了一个应用程序接口。如果您有频道ID,您可以使用搜索API获取当前活动的Live Video ID。

使用以下API:

代码语言:javascript
复制
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,如果您有频道的用户名,可以使用以下请求。

代码语言:javascript
复制
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={CHANNEL_USER_NAME}&key={YOUR_API_KEY}
票数 15
EN

Stack Overflow用户

发布于 2017-07-04 16:04:46

返回直播流ID

代码语言:javascript
复制
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={CHANNEL_ID}&eventType=live&type=video&key={YOUR_API_KEY}

您还可以将查询传递给YouTube,以便YouTube为您提供所有实况流事件

代码语言:javascript
复制
https://www.googleapis.com/youtube/v3/search?part=snippet&q={YOUR_SEARCH_QuERY}&eventType=live&type=video&key={YOUR_YOUTUBE_API}

这就是我如何在我的应用程序中获取实时流视频

代码语言:javascript
复制
    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键

代码语言:javascript
复制
https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=lUOhCtXPN40&format=json

下面是这个的代码

代码语言:javascript
复制
     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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37521853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档