How can i show youtube video in exo player?

Viewed 617

I want to show my youtube video in Exo player with custom controls in android with java and I also want to publish that app on the google play store. So can you please help me with a genuine way of doing this? So my app will not rejected soon when I upload that on google playstore

Thanks in Advance

2 Answers

First of all, you need not worry about you app being rejected because until it follows googles policies, it won't remove your app.

And the requirement you want can be fulfilled with this library. This will not increase app size more than 1 MB. It also it customisable just like you want.

This is how you can implement it.

1.In your build.gradle, add this dependency

implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5'

2.In your layout xml file add the following code

<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
    android:id="@+id/youtube_player_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:autoPlay="true"
    app:showFullScreenButton="false" />

3.In your activity add the following code

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

4.To load the video add this

youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
  @Override
  public void onReady(@NonNull YouTubePlayer youTubePlayer) {
      //if your url is something like this -> https://www.youtube.com/watch?v=EzyXVfyx7CU
      String urlToLoad = "https://www.youtube.com/watch?v=EzyXVfyx7CU";
      String[] url = urlToLoad.split("watch?v=");
      youTubePlayer.loadVideo(url[1], 0);
      //if your url is something like this -> EzyXVfyx7CU
      String videoId = "EzyXVfyx7CU";
      youTubePlayer.loadVideo(videoId, 0);
  }
});

Now to add customisations, you can follow the following links.

Toggle auto link

Enable automatic initialization

Handle UI Events

Add Web UI

Toggle live video UI

Show YouTube Button

Toggle Full Screen

Show video current time

Show video description

Show seekbar

If you are thinking that you can fully customise the UI,you cannot do it any way.This library also provides only option on the left and right side and the top and bottom of the pause/play button.It will look something like this.

image

you can easily use Youtube url extractor repo to extract the mp4 url from youtube url than you can easily play that youtube video in exo player

Related