【发布时间】:2020-09-17 13:06:28
【问题描述】:
我想在 android 中播放音频文件列表。您可能知道,当您有视频文件时,使用com.google.android.exoplayer2.ui.PlayerView 确实有意义,因为您可以在屏幕上看到视频的内容,而底部有播放/暂停等控制器。
但是对于音频文件,如果您让播放音频文件,则会出现黑屏。
我需要的是只使用没有黑屏的控制器。
有没有办法做到这一点?
第一次编辑:
在文档中,我遇到了PlaybackControlView 和PlayerControlView。我可以根据我的情况使用它们吗?如果是,使用哪一个?
第二次编辑:这是我研究 1 天后的结果。我在我的布局中使用了PlaybackControlView,如下所示:
<com.google.android.exoplayer2.ui.PlaybackControlView
android:id="@+id/play_back_control_view"
android:layout_width="0dp" <--- I use ConstraintLayout, so this is okay
android:layout_height="wrap_content"
android:background="@color/black"
android:alpha="0.15"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:controller_layout_id="@layout/custom_playback_control_view" />
正如您从上面的布局中看到的那样,我为此使用了一个名为 custom_playback_control_view.xml 的自定义布局。这是我的自定义布局内容:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
xmlns:tools="http://schemas.android.com/tools">
<ImageButton
android:id="@id/exo_play"
style="@style/ExoMediaButton.Play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription"/>
<ImageButton android:id="@id/exo_pause"
style="@style/ExoMediaButton.Pause"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription"/>
<View
android:id="@id/exo_progress_placeholder"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="26dp"
app:layout_constraintEnd_toStartOf="@id/exo_duration"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/exo_play"/>
<TextView
android:id="@id/exo_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textColor="#FFBEBEBE"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/exo_progress_placeholder"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
最后但同样重要的是,我在 Fragment 中将 ExoPlayer 附加到 PlaybackControlView 上:
myAudioPlayer = ExoPlayerFactory.newSimpleInstance(context)
binding.playBackControlView.player = myAudioPlayer
【问题讨论】:
-
你能详细说明一下屏幕截图吗?
-
你想达到什么目的?
-
我只想使用 ExoPlayer 的控制器视图来播放音频文件。就这样。只有底部的控制栏。
-
您需要实现自定义控制器并将播放器附加到您的自定义控制器。你能粘贴你当前的实现吗?
-
@crack_head:是的,当然。我将我的进度作为第二次编辑添加到上面的问题部分。