How to get Youtube Live Stream by Channel Id in Youtube API V3 in android?

三世轮回 提交于 2019-11-30 07:04:50

I have faced the same problem and managed to figure out a solution for the same. YouTube actually provides an API for this purpose. You can use the search API to get the currently active Live Video ID, if you have the channel ID.

Use the following API:

https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={YOUR_CHANNEL_ID}&eventType=live&type=video&key={YOUR_API_KEY}

The key factor here is you have to set the eventType to live and type to video.

For getting the channel ID, you can use the following request, if you have the channel's username.

https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={CHANNEL_USER_NAME}&key={YOUR_API_KEY}

This will return live stream ID

https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={CHANNEL_ID}&eventType=live&type=video&key={YOUR_API_KEY}

You can also pass Query to YouTube so YouTube give you all live stream events

https://www.googleapis.com/youtube/v3/search?part=snippet&q={YOUR_SEARCH_QuERY}&eventType=live&type=video&key={YOUR_YOUTUBE_API}

This how i get live stream videos in my app

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

And if you have YouTube links maybe you store your in database then this will return you some usefull info with out API key

https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=lUOhCtXPN40&format=json

and below code for this

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

and last if you want to see source code here is link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!