自定义ExoPlayer控制功能



在我的活动中,我在ViewPager2中播放视频,这样用户就可以滑动以获得新的视频。我想更改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>

在视图寻呼机的适配器中,我向片段传递一个布尔值,以检查视图寻呼机当前项是否是最后一个

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

我希望它能帮助到别人。

最新更新