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

前端 未结 2 563
我寻月下人不归
我寻月下人不归 2020-12-14 21:09

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

相关标签:
2条回答
  • 2020-12-14 21:52

    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

    0 讨论(0)
  • 2020-12-14 21:55

    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}
    
    0 讨论(0)
提交回复
热议问题