Customizing the ExoPlayer's UI is pretty simple. If you look at the ExoPlayer source, the layout res directory contains the file exo_player_control_view.xml
that points to (includes) another layout - exo_playback_control_view
.
- Copy the contents of the layout resource -
exo_playback_control_view.xml
- Create a Layout Resource file with any name of your choice - eg:
custom_exo_playback_control_view.xml
. Paste the copied contents to the new xml
- Make whatever changes to the UI widgets as required. Don't change the Id attribute of any widget/control.
- Point the custom exoPlayer UI resource in your layout file that contains the
PlayerView
.
Here is the custom_exo_controller_view
xml that I created from the original. I wanted a different background color for the player controls and different button and progress view colors:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/attachment_document_desc_bg"
android:layoutDirection="ltr"
android:orientation="vertical"
android:paddingStart="20dp"
android:paddingEnd="20dp"
tools:targetApi="28">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="4dp">
<ImageButton
android:id="@id/exo_prev"
style="@style/ExoMediaButton.Previous"
android:tint="@color/colorPrimaryDark"
android:tintMode="src_in" />
<ImageButton
android:id="@id/exo_rew"
style="@style/ExoMediaButton.Rewind"
android:tint="@color/colorPrimaryDark"
android:tintMode="src_in" />
<ImageButton
android:id="@id/exo_shuffle"
style="@style/ExoMediaButton.Shuffle"
android:tint="@color/colorPrimaryDark"
android:tintMode="src_in" />
<ImageButton
android:id="@id/exo_repeat_toggle"
style="@style/ExoMediaButton"
android:tint="@color/colorPrimaryDark"
android:tintMode="src_in" />
<ImageButton
android:id="@id/exo_play"
style="@style/ExoMediaButton.Play"
android:tint="@color/colorPrimaryDark"
android:tintMode="src_in" />
<ImageButton
android:id="@id/exo_pause"
style="@style/ExoMediaButton.Pause"
android:tint="@color/colorPrimaryDark"
android:tintMode="src_in" />
<ImageButton
android:id="@id/exo_ffwd"
style="@style/ExoMediaButton.FastForward"
android:tint="@color/colorPrimaryDark"
android:tintMode="src_in" />
<ImageButton
android:id="@id/exo_next"
style="@style/ExoMediaButton.Next"
android:tint="@color/colorPrimaryDark"
android:tintMode="src_in" />
<ImageButton
android:id="@id/exo_vr"
style="@style/ExoMediaButton.VR"
android:tint="@color/colorPrimaryDark"
android:tintMode="src_in" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@id/exo_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#ff323232"
android:textSize="14sp"
android:textStyle="bold" />
<com.google.android.exoplayer2.ui.DefaultTimeBar
android:id="@id/exo_progress"
android:layout_width="0dp"
android:layout_height="26dp"
android:layout_weight="1"
app:played_color="@color/colorPrimaryDark"
app:unplayed_color="@color/gray" />
<TextView
android:id="@id/exo_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#ff323232"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
Below is the code that I use for the player. Notice, the xml attribute app:controller_layout_id
points to the custom controller view thats defined above.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:auto_show="true"
app:controller_layout_id="@layout/custom_exo_controller_view"
app:fastforward_increment="10000"
app:repeat_toggle_modes="none"
app:resize_mode="fixed_width"
app:rewind_increment="10000"
app:surface_type="surface_view"
app:use_controller="true" />
</FrameLayout>
That should get the UI the way you want.
You can add extra controls as you need in the custom controller and in your code, find the control with findViewById()
and write event listeners, etc.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…