Siavash Abdoli
Siavash Abdoli

Reputation: 1862

Custom UI on exoplayer sample

** I really need help if you don't know anything don't give me a negative point :| if something bother you comment**

I want to write custom UI for my player in Exoplayer(change button of pause play or add new buttons like player speed next and etc) .

I use Exoplayer sample from github and before add code to my original project, I want to test the custom UI on official sample.

I read pages in Stackoverflow and tuts+ about custom UI but I really confused!

why change some buttons image or change their place must be so difficult :) how i can handle this?

EDIT

this is the sample https://github.com/google/ExoPlayer/tree/master/demo

I read these two article:

http://www.brightec.co.uk/ideas/custom-android-media-controller

http://code.tutsplus.com/tutorials/create-a-music-player-on-android-user-controls--mobile-22787

according to this link "Instead of writing your own media controller from scratch, you could start with the MediaController class that is included in Android" and I ask this question because i can't do this steps on exoplayer library and tutorial is written for default media player

Upvotes: 24

Views: 57458

Answers (3)

Ram Iyer
Ram Iyer

Reputation: 1679

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.

  1. Copy the contents of the layout resource - exo_playback_control_view.xml
  2. 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
  3. Make whatever changes to the UI widgets as required. Don't change the Id attribute of any widget/control.
  4. 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.

Upvotes: 42

Sough
Sough

Reputation: 363

ok, that took me half a day, until i didn't dive into the library code and found such text:

To customize the layout of PlaybackControlView throughout your app, or just for certain configurations, you can define exo_playback_control_view.xml layout file in your application res/layout directories. These layouts will override the one provided by the ExoPlayer library, and will be inflated for use by PlaybackControlView.

Full text: https://github.com/google/ExoPlayer/blob/release-v2/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlaybackControlView.java

Also, want to add that u can specify custom layout in PlayerView like so:

<com.google.android.exoplayer2.ui.SimpleExoPlayerView android:id="@+id/player_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:controller_layout_id="@layout/custom_controls"/>

Upvotes: 21

Liuting
Liuting

Reputation: 1098

Actually the code of involking play & pause methods are in class PlayerControl.

If you don't want to use default Android media controller UI, just don't use the MediaController class, create your own UI in your layout file with custom play & pause button and bind the actions with the button's onClickListener.

To play the video, call exoPlayer.setPlayWhenReady(true);

And to pause: call exoPlayer.setPlayWhenReady(false);

It's similar problem with how to create custom UI for android MediaController

Upvotes: 11

Related Questions