6

Through XML we can easily add custom UI controller (controller_layout_id) to SimpleExoPlayerView, like this:

    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/exo_player_view"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        app:controller_layout_id="@layout/player_controls"/>

Is there a way to add such layout programmatically?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zeero0
  • 2,602
  • 1
  • 21
  • 36

2 Answers2

4

There is no such method at this moment which allows you to directly set a custom controller on playerView. instead you have to add your controller to playerview in xml and the inflate it.

In my case I did the following for adding the controller (I assume, you are not planning to change controller while playing the video or after inflating it)

Step 1: Define your xml with just playerview and controller

 //simple_exo_player.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.exoplayer2.ui.PlayerView
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/playerView"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     app:rewind_increment="10000"
     app:fastforward_increment="10000"
     android:background="#000"
     app:show_timeout="1000"
     app:controller_layout_id="@layout/custom_playback_control"
     app:resize_mode="fit">

Step 2: Inflate it and get the PlayerView

 PlayerView customPlayerView;
 View view = LayoutInflater.from(applicationContext).inflate(R.layout.simple_exo_player, null, false);`enter code here`   
 customPlayerView = (PlayerView) view.getRootView();

As you haven't mentioned the reason why you wanted to add it programmatically, I will assume the following.

  1. You want to have multiple custom controller, and use it based on view type
  2. you are planning to use it in recyclerview(or multiple view set) were instead of having playerview with custom controller already predefined in xml, you want to add it programatically

here's what you have to do:

create multiple layout files of playerview with different controllers by changing the line

app:controller_layout_id="your custom controller"

now get your custom playerview as written in step 2

add it to the your recyclerview at required position

FrameLayout playArea;
playArea = v.findViewById(R.id.exoplayer_frame);
playArea.addView(customPlayerView);
Extremis II
  • 5,185
  • 2
  • 19
  • 29
  • whoever wants to downvote, plz mention what didn't work for you, so that answer can be improved, updated or removed(if the library has significant updates for which this answer doesn't work) – Extremis II Aug 15 '19 at 14:26
  • can i set custom controls outside of the Playerview, Like playerview at the top of screen and custom controls should display at bottom ??? Is it possible ?? – Gyan Swaroop Awasthi May 03 '21 at 13:37
  • @GyanSwaroopAwasthi Yes, you can customize the position of playerview. There are many articles that can help you achieve this. Here is one https://levelup.gitconnected.com/customize-exoplayer-overlay-look-like-youtube-player-14fdd6d4583d – Extremis II May 03 '21 at 17:31
0

According to javadoc, we have none of corresponding method. http://google.github.io/ExoPlayer/doc/reference/com/google/android/exoplayer2/ui/SimpleExoPlayerView.html

  • controller_layout_id - Specifies the id of the layout resource to be inflated by the child PlaybackControlView. See below for more details.
  • Corresponding method: None
  • Default: R.id.exo_playback_control_view

And also we cannot inherit the SimpleExoPlayerView because its definition is final.

What are you trying to achieve through programmatically adding? Is it just that?

galcyurio
  • 1,776
  • 15
  • 25