【问题标题】:Custom ExoPlayer control functions自定义 ExoPlayer 控制功能
【发布时间】:2020-12-30 21:30:28
【问题描述】:

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

【问题讨论】:

    标签: android exoplayer2.x


    【解决方案1】:

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

    我从 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>
    

    片段.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);
    
                }
            });
    

    我希望它对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-09-30
      • 2012-10-29
      • 2012-11-08
      • 2020-01-07
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多