BottomSheetDialog 中 VideoView 的 MediaController 隐藏在 BottomSheetDialog 后面

MediaController for VideoView inside BottomSheetDialog is hidden behind the BottomSheetDialog

我有一个项目,我在其中以编程方式将自定义地图标记添加到地图 activity。当用户单击标记时,屏幕底部会出现一个 BottomSheetDialog,其中包含有关自定义地图标记的信息。关键信息之一是将要播放的视频。问题是 VideoView 的 MediaController 没有悬停在视频上方,而是在 BottomSheetDialog 后面。它在屏幕上的正确位置,但位于 BottomSheetDialog 和 VideoView 之后。我正在使用 EpoxyRecyclerView 将 VideoView、MediaController 和各种 TextView 添加到 BottomSheetDialog。

创建 BottomSheetDialog 的函数:

override fun onMarkerClick(marker: Marker): Boolean {
    val modalSheetView = layoutInflater.inflate(R.layout.activity_page_details, null)
    val bottomSheetDialog = BottomSheetDialog(this)
    bottomSheetDialog.setContentView(modalSheetView)

    pageController.page = marker.tag as Page

    val epoxyRecyclerView = bottomSheetDialog.findViewById<EpoxyRecyclerView>(R.id.pageDetailsRecyclerView)
    epoxyRecyclerView?.setControllerAndBuildModels(pageController)

    bottomSheetDialog.show()

    bottomSheetDialog.setOnDismissListener { it.dismiss() }

    // Return false to indicate that we have not consumed the event and that we wish
    // for the default behavior to occur (which is for the camera to move such that the
    // marker is centered and for the marker's info window to open, if it has one).
    return false
}

将 VideoView 和 MediaController 添加到 BottomSheetDialog 的 Epoxy 函数:

data class PageVideoEpoxyModel(
    val videoUrl: String
):ViewBindingKotlinModel<ActivityPageVideoBinding>(R.layout.activity_page_video){

    override fun ActivityPageVideoBinding.bind() {
        videoView.setVideoURI(Uri.parse(videoUrl))
        videoView.setOnPreparedListener {

            val mediaController = MediaController(videoView.context)

            mediaController.setMediaPlayer(videoView)
            mediaController.isEnabled = true

            videoView.setMediaController(mediaController)

            mediaController.setAnchorView(videoView)
            mediaController.requestFocus()

            videoView.start()
            mediaController.show(0)
        }
    }
}

VideoView 的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="210dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:clickable="true"/>

</androidx.constraintlayout.widget.ConstraintLayout>

EpoxyRecycler 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<com.airbnb.epoxy.EpoxyRecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pageDetailsRecyclerView"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

下面的版本还可以,但是我使用第三方库ExoPlayer彻底解决了问题,解决了所有问题。我删除了我的基本 VideoView 并将其替换为 ExoPlayerVideoView,现在该应用程序可以使用零问题。

我设法通过在 VideoView 的布局文件中添加 FrameLayout 来解决此问题:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content">

<VideoView
    android:id="@+id/videoView"
    android:layout_width="wrap_content"
    android:layout_height="210dp"
    android:layout_marginTop="16dp"
    android:clickable="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<FrameLayout
    android:id="@+id/frameView"
    android:layout_width="@id/videoView"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:layout_constraintStart_toStartOf="@id/videoView"
    app:layout_constraintEnd_toEndOf="@id/videoView"
    app:layout_constraintBottom_toBottomOf="@id/videoView"/>

</androidx.constraintlayout.widget.ConstraintLayout>

然后从其父视图中删除 MediaController 并将其添加到 FrameView。在 EpoxyController 中构建 VideoView 的方法中的代码:

data class PageVideoEpoxyModel(
    val videoUrl: String,
):ViewBindingKotlinModel<ActivityPageVideoBinding>(R.layout.activity_page_video){

    override fun ActivityPageVideoBinding.bind() {
        videoView.setVideoURI(Uri.parse(videoUrl))
        videoView.setOnPreparedListener {

            val mediaController = MediaController(videoView.context)
            mediaController.requestFocus()

            val parent = mediaController.parent as ViewGroup
                parent.removeView(mediaController)

            frameView.addView(mediaController)

            mediaController.setMediaPlayer(videoView)
            mediaController.isEnabled = true

            videoView.setMediaController(mediaController)

            mediaController.setAnchorView(videoView)

            videoView.start()
            mediaController.show(0)
        }
    }
}