Search code examples
androidyoutubeyoutube-apiyoutubeplayer

Android YouTube Player Livestream


I am very new to Android Dev, Java, and in general - so bear with me. I have been experimenting with GitHub PierfrancescoSoffritti android-youtube-player This works very well as a customizable open source YouTube player for Android, with one exception.

The main code uses a videoID to play :

YouTubePlayerView youTubePlayerView = findViewById(R.id.youtube_player_view);
getLifecycle().addObserver(youTubePlayerView);

youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
  @Override
  public void onReady(@NonNull YouTubePlayer youTubePlayer) {
    String videoId = "xxxx";
    youTubePlayer.loadVideo(videoId, 0);
  }
});

The problem is, I would like to use it for a YouTube Livestream, where the videoId can change if the stream is ever recreated.

YouTube offers a way around this if you are embedding in a webpage, by not using the videoID, but a channelID. That way, it will always embed the correct currently-playing livestream without having to use the VideoID.

https://www.youtube.com/embed/live_stream?channel=ChannelID

I know there is a YouTube API that can somehow return a currently playing livestream videoID from a channelID along the lines of:

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

But here is where I am stuck. Any idea how to get an updated livestream videoId from a channelId and then use it as String videoId = "xxxx"; in the PierfrancescoSoffritti android-youtube-player?

public void onReady(@NonNull YouTubePlayer youTubePlayer) {
    String videoId = "xxxx";
    youTubePlayer.loadVideo(videoId, 0);

Solution

  • you are basically already answering your own question.

    First you need to get the videoID of your livestream and for that you need a youtubeAPI key that afaic you dont need for pierfrancesco's library. The following video explains how to get it. please be mindful of the restrictions you place on the API key as it wont work if its too restricted (its probably best to not restrict it at all at first). Then you fill in the channelID and API key into the url you so kindly provided

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

    you probably already know this but you can use something such as Volley in order to make the http request. In return you will get a JSONObject that looks as follows:

    {
      "kind": "youtube#searchListResponse",
      "etag": "kmbaHcb5CMLZ5VevVM",
      "regionCode": "NI",
      "pageInfo": {
        "totalResults": 1,
        "resultsPerPage": 1
      },
      "items": [
        {
          "kind": "youtube#searchResult",
          "etag": "RkUpUQpSKztQlA",
          "id": {
            "kind": "youtube#video",
            "videoId": "GLdex45V_RQ"// <------------------------- HERE IT IS
          },
          "snippet": {
            "publishedAt": "2021-10-14T21:05:37Z",
            "channelId": "UCeY0bbntWzzVIaj2z3QigXg",
            "title": "LIVE: NBC News NOW - October 15",
            "description": "NBC News NOW is live, reporting breaking news and developing stories in real time. We are on the scene, covering the most important stories of the day and ...",
            "thumbnails": {
              "default": {
                "url": "https://i.ytimg.com/vi/GLdex45V_RQ/default_live.jpg",
                "width": 120,
                "height": 90
              },
              "medium": {
                "url": "https://i.ytimg.com/vi/GLdex45V_RQ/mqdefault_live.jpg",
                "width": 320,
                "height": 180
              },
              "high": {
                "url": "https://i.ytimg.com/vi/GLdex45V_RQ/hqdefault_live.jpg",
                "width": 480,
                "height": 360
              }
            },
            "channelTitle": "NBC News",
            "liveBroadcastContent": "live",
            "publishTime": "2021-10-14T21:05:37Z"
          }
        }
      ]
    }
    

    and there you can find the videoID you are looking for by diving into that object.

    After that you just pass it to pierfrancescos's loadVideo() method and voila. If the liveStream fails or is recreated (which I bet wont happen too often) the user can fix it by simply reopening the app (since then a new request is launched to googleapi.com) Most likely, however, one of pierfrancescos error catching callback methods (like onError()) would be triggered allowing you to seamlessly relaunch a request and reload the video under the new videoID with minimal disruption.

    let me know if this works for you