android - 如何将 Exoplayer 的控件放置在 PlayerView 之外

标签 android exoplayer exoplayer2.x

我正在使用 Exoplayer2 在我的应用程序中显示视频。我需要控件始终可见。我可以通过设置 app:show_timeout="0" 来存档此内容。但是,当控件始终可见时,它们会占用 PlayerView 中的空间。

我想显示 PlayerView 下方的控件,以便我始终可以显示整个视频。

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
    tools:context=".MediaPlayerActivity"
    android:theme="@android:style/Theme.NoTitleBar"
    android:contentDescription="hf_hide_help">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:use_controller="true"
        app:rewind_increment="0"
        app:fastforward_increment="0"
        app:repeat_toggle_modes="none"
        app:show_timeout="0"
        />
</RelativeLayout>

这是我的exo_player_control_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layoutDirection="ltr"
    android:background="#CC000000"
    android:orientation="vertical">

    <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_play"
            style="@style/ExoMediaButton.Play"/>

        <ImageButton android:id="@id/exo_pause"
            style="@style/ExoMediaButton.Pause"/>

        <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"/>

        <com.google.android.exoplayer2.ui.DefaultTimeBar
            android:id="@id/exo_progress"
            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>

它是这样的: Player

这就是当我单击播放器时的样子 - 控件隐藏。 Player without controls

最佳答案

您的用例不是通用的,ExoPlayer 不支持它。在您的情况下,您将需要禁用默认控件并在播放器 View 下添加您自己的自定义控件。构建布局并向其中添加操作将非常容易。

更新:(回答 Gyan 关于时间栏的评论)

两种方法:(我解释了这些策略,因为 SO 和其他网站上已经有数千个代码示例)

1 - 我的建议非常适合我的需求: 覆盖 exoplayer 的默认控件 View (在布局文件夹中有一个同名的 View 就可以完成这项工作)。然后删除您在自定义 View 中按下的所有不必要的按钮只需将时间栏保留在覆盖 View 中即可。通过这样做,所有出现/消失的行为都会自动保留在时间栏中,其余的都在您自己的 View 中。如果您喜欢这种方法,则无需担心如何处理时间栏。

2-以防万一您坚持将时间栏移到播放器窗口下方,您需要的是水平进度条(请注意,您可以自定义进度 View 的外观以匹配您的设计)。然后在 exoplayer 上编写一个监听器来获取计时(已过/剩余/当前播放时间)并根据它们计算和更新进度条。请记住,进度条需要可触摸(并且可聚焦),以便用户可以来回拖动它。那么当用户拖动进度时,您需要重新计算播放时间。 (显然有大量的编码和计算,但几乎是简单的事情)。希望对你有帮助。如果您需要代码示例,请告诉我。

关于android - 如何将 Exoplayer 的控件放置在 PlayerView 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51856565/

相关文章:

android - 更改playerNotificationManager的操作图标,例如播放/暂停/停止

Android - 如何将所有屏幕尺寸和密度的图标放在全屏图像上的同一位置?

java - 即使 PlayerView 已暂停,Exoplayer 也会播放音频

android - 无法使用 Android Exoplayer react-native-video 在 Android 上播放视频 H.264

exoplayer - 如何在 google IO18 audio App demo 中添加播放器控制 ui

android - 如何在 Exoplayer 2 中播放 .ts

android - 从eclipse构建apk文件的问题

android - Gmail like contact selector/tag cloud/chip cloud with profile image 安卓

java - 每秒减少一个整数

android - 同时播放多个视频 ExoPlayer