我正在使用播放器视图,我想显示视频的进度。所以我使用了PlayerView并在playerview下面使用了LinearLayout,在LinearLayout内部我使用了PlayerView
我的代码如下:
<LinearLayout 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:background="@android:color/black"
android:orientation="vertical">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@android:color/white"
android:visibility="visible">
</com.google.android.exoplayer2.ui.PlayerView>
<com.google.android.exoplayer2.ui.DefaultTimeBar
android:id="@+id/exo_progress1"
style="@style/Base.Widget.AppCompat.SeekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
这是我的要求
对于这种情况我有一个解决方法。这个想法是,您仍然将 DefaultTimeBar 放在 PlayerView 控制器布局内,然后手动将 DefaultTimeBar 视图移动到 PlayerView 外部,对于您的情况,它位于 LinearLayout 中。
R.layout.main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@android:color/white"
app:controller_layout_id="@layout/exo_playback_control_view" />
</LinearLayout>
R.layout.exo_playback_control_view
<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">
<com.google.android.exoplayer2.ui.DefaultTimeBar
android:id="@+id/exo_progress"
style="@style/Base.Widget.AppCompat.SeekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
然后,在您的活动/片段中的某个位置,手动移动视图(例如,我在这里使用 Kotlin)。
val mainView = findViewById<LinearLayout>(R.id.mainView)
val videoView = findViewById<PlayerView>(R.id.videoView)
val defaultTimeBarView = videoView.findViewById<DefaultTimeBar>(R.id.exo_progress)
val parent = defaultTimeBarView.parent as? ViewGroup
parent?.removeView(defaultTimeBarView)
mainView.addView(defaultTimeBarView)
但是,如果 PlayerView 的控制器视图可见性被隐藏,则进度条上仍然有奇怪的动画,但如果它可见,则进度条可以工作。