首页 > 解决方案 > exoplayer 控制器的自定义逻辑

问题描述

我已经设法为我的 exoplayer 创建了一个自定义控制器。在这个布局中,我放置了一些按钮和一些我希望能够控制的其他元素(添加一些事件、更改文本等......)。我必须扩展什么课程?如何将控制器与我的自定义逻辑“链接”?这是我到目前为止所做的:

<com.google.android.exoplayer2.ui.PlayerView
            android:id="@+id/player"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            app:show_buffering="when_playing"
            app:controller_layout_id="@layout/customController"/>

在 customController.xml 我有这样的东西:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:orientation="vertical">
<TextView
    android:id="@+id/fileName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change me!"/>

谢谢!

标签: androidcontrollerexoplayer

解决方案


您应该将自定义布局按钮 ID 更改为 exo 播放器使用的默认 ID。然后在exo播放器视图中设置自定义布局。你已经正确设置了。只需要更改ID。然后它将起作用。

当 PlayerView 被实例化时,它会从布局文件 exo_player_view.xml 中扩展其布局。PlayerControlView 从 exo_player_control_view.xml 扩展其布局。要自定义这些布局,应用程序可以在其自己的 res/layout* 目录中定义具有相同名称的布局文件。这些布局文件覆盖了 ExoPlayer 库提供的那些。

例如,假设我们希望播放控件仅包含位于视图中心的播放/暂停按钮。我们可以通过在应用程序的 res/layout 目录中创建 exo_player_control_view.xml 文件来实现这一点,

所以现在您已经创建了自定义布局。现在将自定义布局中所有ui元素的id改为exo播放器控制器布局使用的原始ID。下面是exo播放器中的原始布局代码。从 id 中获取默认 id 并粘贴到您的自定义布局中。

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layoutDirection="ltr" android:background="#CC000000" android:orientation="vertical" tools:targetApi="28">
   <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="4dp" android:orientation="horizontal">
      <ImageButton android:id="@id/exo_prev" style="@style/ExoMediaButton.Previous" />
      <ImageButton android:id="@id/exo_rew" style="@style/ExoMediaButton.Rewind" />
      <ImageButton android:id="@id/exo_shuffle" style="@style/ExoMediaButton" />
      <ImageButton android:id="@id/exo_repeat_toggle" style="@style/ExoMediaButton" />
      <ImageButton android:id="@id/exo_play" style="@style/ExoMediaButton.Play" />
      <ImageButton android:id="@id/exo_pause" style="@style/ExoMediaButton.Pause" />
      <ImageButton android:id="@id/exo_ffwd" style="@style/ExoMediaButton.FastForward" />
      <ImageButton android:id="@id/exo_next" style="@style/ExoMediaButton.Next" />
      <ImageButton android:id="@id/exo_vr" style="@style/ExoMediaButton.VR" />
   </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:textSize="14sp" android:textStyle="bold" android:paddingLeft="4dp" android:paddingRight="4dp" android:includeFontPadding="false" android:textColor="#FFBEBEBE" />
      <View android:id="@id/exo_progress_placeholder" android:layout_width="0dp" android:layout_weight="1" android:layout_height="26dp" />
      <TextView android:id="@id/exo_duration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:textStyle="bold" android:paddingLeft="4dp" android:paddingRight="4dp" android:includeFontPadding="false" android:textColor="#FFBEBEBE" />
   </LinearLayout>
</LinearLayout>

在活动中,您可以通过 findViewById 方法找到播放器视图。

PlayerView playerView = findViewById(R.id.video_view);

然后只需将媒体播放器设置为播放器视图

Simple Exoplayer  player = ExoPlayerFactory.newSimpleInstance(this); playerView.setPlayer(player); 

现在你可以从你的playerView参考做所有事情。无需扩展类和做其他事情。并记住在开始时启动玩家并在停止时关闭..

文档链接如下。您可以在那里找到更多详细信息。

Exo 播放器文档链接

如果您想扩展类,请使用下面的示例。但我认为没有必要这样做。您可以附加播放器状态侦听器,也可以播放暂停做从引用到播放器的所有事情。

扩展类示例链接


推荐阅读