我使用 ExoPlayer 在我的 Android 应用程序上播放音频,并使用 SimpleExoPlayerView 作为控制器。默认控制器有五个按钮,播放/暂停、前进、后退、下一个和上一个。我的应用程序只使用播放/暂停、前进和后退,所以我想从控制器中删除下一个和上一个按钮,但我找不到使用 SimpleExoPlayerView 的方法。我如何实现这一目标?
每个按钮都有默认样式。只需覆盖 Previous 和 Next 按钮的样式并添加可见性即可。
在您的 style.xml 中放置以下样式
<style name="ExoMediaButton.Previous">
<item name="android:visibility">gone</item>
</style>
<style name="ExoMediaButton.Next">
<item name="android:visibility">gone</item>
</style>
您需要一个自定义布局
exo_playback_control_view.xml
文件,这是exoplayer
默认寻找的用于膨胀控制视图的文件。 重要的是自定义布局被命名为exo_playback_control_view
并且使用了某些id
s
默认控件可以在源代码中看到here release-v2或here dev-v2-r2.3.1(确保找到您正在使用的
exoplayer
版本)
您可以将该文件复制到您的
res/layout
目录,并删除不需要的按钮,这是默认的样子:
<ImageButton android:id="@id/exo_prev"
style="@style/ExoMediaButton.Previous"/>
<ImageButton android:id="@id/exo_rew"
style="@style/ExoMediaButton.Rewind"/>
<ImageButton android:id="@id/exo_play"
style="@style/ExoMediaButton.Play"/>
<ImageButton android:id="@id/exo_pause"
style="@style/ExoMediaButton.Pause"/>
<ImageButton android:id="@id/exo_ffwd"
style="@style/ExoMediaButton.FastForward"/>
<ImageButton android:id="@id/exo_next"
style="@style/ExoMediaButton.Next"/>
exoplayer
有很大的帮助;作者写了一个自定义控件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton android:id="@id/exo_play"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#CC000000"
style="@style/ExoMediaButton.Play"/>
<ImageButton android:id="@id/exo_pause"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#CC000000"
style="@style/ExoMediaButton.Pause"/>
</FrameLayout>
注意
id
s是required
在你的 style.xml 中尝试以下内容
<style name="ExoMediaButton.Previous">
<item name="android:layout_height">0dp</item>
<item name="android:layout_width">0dp</item>
</style>
<style name="ExoMediaButton.Next">
<item name="android:layout_height">0dp</item>
<item name="android:layout_width">0dp</item>
</style>
exoplayer的PlayerView类有两个隐藏方法
playerView.setShowNextButton(showNextPrevButtons)
playerView.setShowPreviousButton(showNextPrevButtons)
注意按钮的可见性是动态变化的
所以最好将它们的大小设置为零以完全隐藏
<ImageButton android:id="@id/exo_prev"
android:layout_width="0dp"
android:layout_height="0dp"
style="@style/ExoMediaButton.Previous"
android:visibility="gone"/>
<ImageButton android:id="@id/exo_rew"
style="@style/ExoMediaButton.Rewind"/>
<ImageButton android:id="@id/exo_shuffle"
style="@style/ExoMediaButton.Shuffle"
android:visibility="gone"/>
<ImageButton android:id="@id/exo_repeat_toggle"
style="@style/ExoMediaButton"
android:visibility="gone"/>
<ImageButton android:id="@id/exo_play"
style="@style/ExoMediaButton.Play"/>
<ImageButton android:id="@id/exo_pause"
style="@style/ExoMediaButton.Pause"/>
<ImageButton android:id="@id/exo_ffwd"
style="@style/ExoMediaButton.FastForward"/>
<ImageButton android:id="@id/exo_next"
android:layout_width="0dp"
android:layout_height="0dp"
style="@style/ExoMediaButton.Next"
android:visibility="gone"/>
<ImageButton android:id="@+id/exo_fullscreen"
android:src="@drawable/fullscreen"
style="@style/ExoMediaButton"/>
<ImageButton android:id="@+id/exo_vr"
style="@style/ExoMediaButton"
android:visibility="gone"/>
你应该
hideControl()
,我也做
CustomSimpleExoPlerView
像SimpleExoPlayerView
.showControl()
.CustomSimpleExoPlayer
你也可以用这个,它对我来说很好用
ExoPlayer player = new ExoPlayer.Builder(context).build();
StyledPlayerView styledPlayerView = viewHolder.binding.idExoPlayerVIew;
MediaItem mediaItem = MediaItem.fromUri(message.getVideoUrl());
styledPlayerView.setPlayer(player);
styledPlayerView.setShowNextButton(false);
styledPlayerView.setShowPreviousButton(false);
player.setMediaItem(mediaItem);
player.prepare();
StyledPlayerView playerView = view.findViewById(R.id.videoPlayer);
playerView.findViewById(R.id.exo_prev).setVisibility(View.GONE);
playerView.findViewById(R.id.exo_next).setVisibility(View.GONE);
这对我有用!
SimpleExoPlayer 已弃用。你应该在这个版本中使用StyledPlayerView:
'com.google.android.exoplayer:exoplayer:2.18.2'
简答:
argType:show_next_button="false"
argType:show_previous_button="false"
长答案:您可以用类似的方式隐藏和显示其他按钮。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:argType="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:keepScreenOn="true"
tools:context=".MainActivity">
<com.google.android.exoplayer2.ui.StyledPlayerView
android:id="@+id/playerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
argType:resize_mode="fixed_width"
argType:show_buffering="when_playing"
argType:show_fastforward_button="false"
argType:show_next_button="false"
argType:show_previous_button="false"
argType:show_rewind_button="false"
argType:show_subtitle_button="true"
argType:use_artwork="true"
argType:use_controller="true">
</com.google.android.exoplayer2.ui.StyledPlayerView>
</androidx.constraintlayout.widget.ConstraintLayout>
成功了:
您可以使用这两种方法从播放器中删除下一个和上一个按钮:
playerView.setShowPreviousButton(false);
playerView.setShowNextButton(false);