ExoPlayer 在视频播放期间不显示播放/暂停

时间:2021-03-23 13:21:35

标签: android kotlin exoplayer

我正在开发可使用 ExoPlayer 播放视频的应用。出于某种原因,当我尝试暂停视频播放器时没有显示播放/暂停图标。我制作了一个仅用于播放/暂停的自定义布局并将其设置在 PlayerView 上。使用 player.playWhenReady = trueplayer.playWhenReady = true 播放/暂停视频。在任何地方都找不到解决方案,看起来我遗漏了一些东西,但不知道是什么。

custom_player_controller.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton android:id="@id/exo_play"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="#CC000000"
        style="@style/ExoMediaButton.Play"/>

    <ImageButton android:id="@id/exo_pause"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="#CC000000"
        style="@style/ExoMediaButton.Pause"/>

</FrameLayout>

我在 RecyclerView 中使用 ExoPlayer,这里是 RecyclerView 的 layout_item.xml

<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"
    android:clickable="true"
    android:focusable="true">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/feed_video_player"
        app:surface_type="texture_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:resize_mode="fixed_width"
        app:controller_layout_id="@layout/custom_player_controller"
        android:paddingBottom="55dp"
        android:background="#000"/>

</RelativeLayout>

然后在 RVAdapter 中播放/暂停视频我正在这样做

setOnTouchListener(object : View.OnTouchListener {
    val gestureDetector: GestureDetector = GestureDetector(context, object:
        GestureDetector.SimpleOnGestureListener() {

        override fun onSingleTapUp(e: MotionEvent?): Boolean {
            super.onSingleTapUp(e)

            if (!player.playWhenReady) {
                player.playWhenReady = true
            } else {
                player.playWhenReady = false
            }

            return true
        }
    })

    override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
        gestureDetector.onTouchEvent(p1)
        return true
    }
})

它会暂停和播放视频,我想要的只是在视频暂停时显示暂停图标。谢谢。

2 个答案:

答案 0 :(得分:1)

要在 Exo Player 中播放和暂停,您将像这样使用它。

Exo Player 处理剩下的事情。

player.play(); //starts the playing process
player.pause(); //pauses the media.
player.setPlayWhenReady //this is not for play and pause functionality. It is designed for starting the playback when the resource is ready.

更新

使用以下播放器布局。

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/feed_video_player"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:background="@android:color/black"
        app:controller_layout_id="@layout/custom_player_controller"
        app:resize_mode="fixed_width"
        app:show_timeout="5000"
        android:paddingBottom="55dp"
        android:background="#000"
        app:use_controller="true" />

答案 1 :(得分:0)

尝试添加

app:use_controller="true"
android:keepScreenOn="true"
android:focusable="true"
app:show_timeout="3000"

到您的播放器视图。