14

I am able to get the list of live stream: https://developers.google.com/youtube/v3/live/docs/liveStreams/list#examples But how to get the live stream status and live stream link or id for particular channel using youtube v3 api in android?

YLS
  • 1,475
  • 2
  • 15
  • 35

2 Answers2

16

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}
Harikrishnan
  • 7,765
  • 13
  • 62
  • 113
  • 2
    This only works for PUBLIC livestreams. Does not work for UNLISTED or PRIVATE livestreams. – jsherk Nov 20 '17 at 00:08
  • My live streaming is PUBLIC and LISTED but still no results on api request https://prnt.sc/p8qce3 – Gaston Coroleu Sep 20 '19 at 14:01
  • @GastonCoroleu I'm having a similar issue. I was using the above code snippet for a while until it stopped working about a week ago. Not too sure as to why – SteppingHat Sep 23 '19 at 01:42
  • @GastonCorole Please verify if there is any limit exceeding issue. I just tried after seeing these comments, it is still working for me. Checked the API documentation, there is no change there as well. – Harikrishnan Oct 01 '19 at 15:21
  • @SteppingHat Please verify as well – Harikrishnan Oct 01 '19 at 15:21
  • @Harikrishnan Usage limits were the first thing I checked. I was always way under the quota as I make a maximum of 5 calls a day due to a rate limiting/caching layer. To further troubleshooting I also created a new property on another account and ran the same query with the same result on the main property. However it seems to be working again for some odd reason as of a few days ago. Not too sure what was going on there – SteppingHat Oct 02 '19 at 04:43
  • I confirm, this api call hasn't been working in September 2019. Now it works, yay! – Ivan Oct 02 '19 at 19:31
  • Looks like this api call is not working anymore can anyone confirm? – Kay Aug 03 '20 at 08:48
  • Im trying to get all live streams for this channel but it returns no resuls https://www.youtube.com/channel/UCXzGWycANrrfyBmzX6JMsvQ/live – Kay Aug 03 '20 at 08:48
7

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

Hamza
  • 2,017
  • 1
  • 19
  • 40