Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UI on exoplayer sample

Tags:

** 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

like image 289
Siavash Abdoli Avatar asked Nov 11 '15 09:11

Siavash Abdoli


People also ask

Does YouTube use ExoPlayer?

Screenshot: The YouTube Android app, which uses ExoPlayer as its video player. ExoPlayer is an app-level media player built on top of low-level media APIs in Android. It is an open source project used by Google apps, including YouTube and Google TV.

What is ExoPlayer v2?

ExoPlayer is an application level media player for Android. It provides an alternative to Android's MediaPlayer API for playing audio and video both locally and over the Internet.


1 Answers

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.

like image 183
Ram Iyer Avatar answered Oct 18 '22 17:10

Ram Iyer