如何在media3播放器视图上播放下一首和上一首歌曲

问题描述 投票:0回答:1

我正在实现一个音频应用程序,我在其中使用 recyclerview 显示了主页片段中的所有歌曲,单击该项目将打开一个详细信息片段,并且将开始播放音频。我在布局上使用 media3 播放器视图来播放音频。在播放器视图中,默认有播放、暂停、搜索以及上一个和下一个按钮。我想实现的是,当单击播放器上的“播放下一首”或“播放上一首”图标时,应从该歌曲列表中播放下一首和上一首歌曲。我怎样才能实现它?

android media-player exoplayer foregroundnotification android-media3
1个回答
0
投票

当你得到歌曲列表时,首先创建一个

MediaItem

val mediaItems = list.map { 
    MediaItem.fromUri(Uri.parse(it))
}

然后,将

mediaItems
添加到播放器:

player.addMediaItems(mediaItems)

现在,在

ClickListener
项上定义一个
RecyclerView
,并将单击的项的索引传递给播放器来播放。

player.seekToDefaultPosition(index)

最后,当用户单击下一个/上一个按钮时,播放器将播放我们添加的媒体项目中的下一个/上一个项目。

此外,您可以使用自定义

Controller Layout
进行自定义,并将所有内容放置在您想要的任何位置。但是您需要使用 Media3 默认 id 来让 ExoPlayer 设置所需的值:

<androidx.media3.ui.PlayerView
    android:id="@+id/player_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:resize_mode="fixed_width"
    app:surface_type="texture_view"
    app:use_controller="true"
    app:auto_show="true"
    app:hide_on_touch="false"
    app:animation_enabled="false"
    app:controller_layout_id="@layout/music_video_player_controllers"
    app:show_buffering="always"/>

您可以通过以下布局示例了解这个想法:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tool="http://schemas.android.com/tools"
    android:id="@+id/controller_layout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="bottom"
    app:cardUseCompatPadding="true"
    app:cardBackgroundColor="@color/charade"
    app:cardCornerRadius="10dp"
    android:forceDarkAllowed="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingVertical="@dimen/_15sdp">

        <androidx.media3.ui.DefaultTimeBar
            android:id="@+id/exo_progress"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginHorizontal="@dimen/_24sdp"
            android:layout_weight="1"
            app:buffered_color="@color/white"
            app:played_color="@color/red"
            app:scrubber_color="@color/red"
            app:unplayed_color="@color/white1"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>

        <TextView
            android:id="@id/exo_position"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:minWidth="@dimen/_39sdp"
            android:textColor="@color/white"
            android:textSize="14sp"
            app:layout_constraintTop_toBottomOf="@+id/exo_progress"
            app:layout_constraintStart_toStartOf="@id/exo_progress"
            tool:text="00:00" />

        <TextView
            android:id="@id/exo_duration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:minWidth="@dimen/_39sdp"
            android:textColor="@color/white"
            android:textSize="14sp"
            app:layout_constraintEnd_toEndOf="@id/exo_progress"
            app:layout_constraintTop_toBottomOf="@+id/exo_progress"
            tool:text="00:00" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_15sdp"
            app:layout_constraintTop_toBottomOf="@id/exo_progress"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="@dimen/_28sdp"
                android:layout_height="@dimen/_28sdp"
                android:id="@+id/exo_ffwd"
                android:layout_marginStart="@dimen/_12sdp"
                app:layout_constraintStart_toEndOf="@+id/play_pause_container"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent">

                <androidx.appcompat.widget.AppCompatImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="1dp"
                    app:srcCompat="@drawable/ic_baseline_seek_forward_24" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textSize="8sp"
                    android:paddingEnd="1dp"
                    android:paddingTop="@dimen/_6sdp"
                    android:text="15"
                    android:textColor="@color/white"
                    android:gravity="center"/>

            </androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/play_pause_container"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent">

                <ImageView
                    android:layout_width="@dimen/_31sdp"
                    android:layout_height="@dimen/_31sdp"
                    android:id="@+id/exo_play_pause"
                    app:srcCompat="@drawable/ic_pause_24"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"/>

            </androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="@dimen/_28sdp"
                android:layout_height="@dimen/_28sdp"
                android:id="@+id/exo_rew"
                android:layout_marginEnd="@dimen/_12sdp"
                app:layout_constraintEnd_toStartOf="@+id/play_pause_container"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent">

                <androidx.appcompat.widget.AppCompatImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:padding="1dp"
                    app:srcCompat="@drawable/ic_baseline_seek_backward_24" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textSize="8sp"
                    android:paddingEnd="1dp"
                    android:paddingTop="@dimen/_6sdp"
                    android:text="15"
                    android:textColor="@color/white"
                    android:gravity="center"/>

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

PS:注意id,比如

exo_position
exo_duration
。 按
ctrl
并单击它们即可查看您可以使用的所有 ID。

© www.soinside.com 2019 - 2024. All rights reserved.