【问题标题】:How to place the controls of an Exoplayer outside of the PlayerView如何将 Exoplayer 的控件放置在 PlayerView 之外
【发布时间】:2018-08-15 09:53:51
【问题描述】:

我正在使用 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>

这是它的样子:

这就是它的样子,当我点击播放器时 - 控件隐藏。

【问题讨论】:

  • 请加个截图好吗?
  • 我添加了截图。
  • 我已经创建了自定义控件 xml 文件并在 xml 文件中添加到 PlayerView 中,例如 app:controller_layout_id="@layout/custom_controls" 但自定义控件显示在 PlayerView 上而不是在播放器视图之外。请指导我如何在 PlayerView 之外显示自定义控件。? ——

标签: android exoplayer exoplayer2.x


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 我已经创建了自定义控件 xml 文件并在 xml 文件中添加到 PlayerView 中,例如 app:controller_layout_id="@layout/custom_controls" 但自定义控件显示在 PlayerView 上而不是在播放器视图之外。请指导我如何在 PlayerView 之外显示自定义控件。?
  • 如果你使用 app:controller_layout_id 添加它,行为将与 exoplayer 的原始控件完全相同。只需将您自己的控制器放在另一个视图中并将其粘贴在播放器视图下方即可。 (例如,您可以有一个垂直线性布局,其中玩家作为第一个孩子,控件视图作为第二个孩子)。希望你明白了
  • 是的,我创建了 Linearlayout (orientation=vertical) 并且 playerview 在里面,现在我添加了第二个孩子作为框架布局和内部框架 laout 我使用了 =====> 并且我从 playerview 中删除了 app:controller_layout_id,现在自定义控件在 playerview 下方可见,但所有控件都已死,不像死 UI 一样处于工作阶段.
  • 除非您在按钮中添加一些代码以执行操作,否则它肯定是一个死用户界面。例如,您的自定义播放按钮将运行这个 sn-p: exoPlayer.playWhenReady = true 并且暂停需要运行: exoPlayer.playWhenReady = false 。你必须能够弄清楚我相信的其余部分
  • 感谢@Masoud Dadashi 非常感谢,现在播放和暂停正在工作,但是如果我使用 com.google.android.exoplayer2.ui.DefaultTimeBar 来显示进度任何解决方案,就像播放一样并暂停?如何管理 DefaultTimeBar 进度?
猜你喜欢
  • 2022-06-28
  • 2019-06-18
  • 2018-09-03
  • 2021-07-28
  • 1970-01-01
  • 2012-06-23
  • 2019-06-10
  • 1970-01-01
  • 2020-05-01
相关资源
最近更新 更多