Sunday, 10 October 2021

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


from Android YouTube Player Livestream

No comments:

Post a Comment