自定义 ExoPlayer 控件函数

Custom ExoPlayer control functions

在我的 activity 中,我在 ViewPager2 中播放视频,这样用户可以通过滑动来获取新视频。我想改变 ExoPlayer 的快进和快退按钮功能来改变 viewpager 的位置。我该怎么做?

我一直在为这个问题寻找一个好的解决方案,但由于找不到任何类似的问题得到解答,我想到了这个:

我从 exoplayer 的布局中删除了除搜索栏之外的所有内容。然后我将那些删除的项目放在片段布局文件中的 PlayerControlView(搜索栏)下方,并为按钮提供新的 ID。这样我就可以将 onClickListeners 添加到按钮,然后我可以从那里更改 ViewPager 中的项目。

exo_player_control_view.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layoutDirection="ltr" android:background="#CC000000" android:orientation="vertical" tools:targetApi="28">
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:gravity="center_vertical" android:orientation="horizontal">
        <TextView android:id="@id/exo_position" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:textStyle="bold" android:paddingLeft="4dp" android:paddingRight="4dp" android:includeFontPadding="false" android:textColor="#FFBEBEBE" />
        <View android:id="@id/exo_progress_placeholder" android:layout_width="0dp" android:layout_weight="1" android:layout_height="26dp" />
        <TextView android:id="@id/exo_duration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:textStyle="bold" android:paddingLeft="4dp" android:paddingRight="4dp" android:includeFontPadding="false" android:textColor="#FFBEBEBE" />
    </LinearLayout>
</LinearLayout>

fragment.xml:

...
<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="@color/colorPrimaryDark">

                <com.google.android.exoplayer2.ui.PlayerView
                    android:id="@+id/play_view"
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentEnd="true"
                    app:surface_type="texture_view"
                    app:use_controller="false"/>

                <com.google.android.exoplayer2.ui.PlayerControlView
                    android:id="@+id/controls"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/play_view"
                    app:show_timeout="0" />

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:paddingTop="4dp"
                    android:orientation="horizontal"
                    android:background="#CC000000"
                    android:layout_below="@id/controls">

                    <ImageButton
                        android:id="@+id/rew"
                        style="@style/ExoMediaButton.Previous"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        android:layout_marginStart="18dp"/>

                    <ImageButton
                        android:id="@+id/play"
                        style="@style/ExoMediaButton.Play"
                        android:visibility="gone"
                        app:layout_constraintEnd_toStartOf="@+id/ffwd"
                        app:layout_constraintStart_toEndOf="@id/rew"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"/>

                    <ImageButton
                        android:id="@+id/pause"
                        style="@style/ExoMediaButton.Pause"
                        app:layout_constraintEnd_toStartOf="@+id/ffwd"
                        app:layout_constraintStart_toEndOf="@id/rew"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"/>

                    <ImageButton
                        android:id="@+id/ffwd"
                        style="@style/ExoMediaButton.Next"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        android:layout_marginEnd="18dp"/>

                </androidx.constraintlayout.widget.ConstraintLayout>

            </RelativeLayout>

在 viewpager 的适配器中,我将一个布尔值传递给片段以检查 viewpager 的当前项目是否是最后一个

...
@Override
        public Fragment createFragment(int position) {
            boolean last=false;
            if (position == mDataBase.size()-1)
                last = true;
            VideoItem video = mDataBase.get(position);
            return VideoPlayerFragment.newInstance(video.getTitle(),last);
        }
...

这是片段的代码:

...

mForward = view.findViewById(R.id.ffwd);
mPlay = view.findViewById(R.id.play);
mRewind = view.findViewById(R.id.rew);
mPause = view.findViewById(R.id.pause);

...

if (getArguments().getBoolean(KEY_LAST)){
            mForward.setAlpha((float) 0.5);
        }

        mPlayer.addListener(new Player.EventListener() {
            @Override
            public void onIsPlayingChanged(boolean isPlaying) {
                if (isPlaying){
                    mPause.setVisibility(View.VISIBLE);
                    mPlay.setVisibility(View.GONE);
                }else{
                    mPause.setVisibility(View.GONE);
                    mPlay.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                if (playbackState == Player.STATE_ENDED){
                    mPause.setVisibility(View.GONE);
                    mPlay.setVisibility(View.VISIBLE);
                    ended = true;
                }else
                    ended = false;
            }
        });


        mPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPlayer.setPlayWhenReady(false);
            }
        });

        mPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ended){
                    mPlayer.seekTo(0);
                }
                mPlayer.setPlayWhenReady(true);
            }
        });
        mForward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!getArguments().getBoolean(KEY_LAST)){
                    mPager.setCurrentItem(mPager.getCurrentItem()+1);
                }
            }
        });

        mRewind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPlayer.getCurrentPosition() < 2000 && mPager.getCurrentItem() != 0)
                    mPager.setCurrentItem(mPager.getCurrentItem()-1);
                else
                    mPlayer.seekTo(0);

            }
        });

希望对大家有所帮助。