要在ExoPlayer中隐藏快退和快进按钮,您需要自定义通知布局以删除这些按钮
res/layout
文件夹中创建一个名为exo_notification_layout.xml
的新布局文件。将以下代码添加到该文件中:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<!-- 添加您的播放控制按钮或其他自定义控件 -->
<ImageButton
android:id="@+id/play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_play"
android:background="@null" />
<!-- 删除快退和快进按钮 -->
</LinearLayout>
resources
文件夹中,找到名为exo_notification_icon_mapping.xml
的文件,然后删除exo_fastforward
和exo_rewind
条目,如下所示:<resources>
<item name="exo_play" type="drawable">@drawable/ic_play</item>
<item name="exo_pause" type="drawable">@drawable/ic_pause</item>
<!-- 删除以下两个条目 -->
<!-- <item name="exo_fastforward" type="drawable">@drawable/ic_fastforward</item> -->
<!-- <item name="exo_rewind" type="drawable">@drawable/ic_rewind</item> -->
</resources>
onCreateNotificationChannel()
方法(如果尚未创建),然后添加以下代码:private void onCreateNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "exo_notification_channel";
String channelName = "ExoPlayer Notification Channel";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}
onCreate()
方法中,初始化PlayerNotificationManager
,并将其与自定义布局关联:private PlayerNotificationManager playerNotificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ... 其他初始化代码 ...
playerNotificationManager = new PlayerNotificationManager.Builder(this, channelId, R.drawable.ic_notification)
.setCustomLayout(getCustomNotificationLayout())
.build();
}
getCustomNotificationLayout()
的方法,并返回您在步骤1中创建的自定义布局文件的ID:private int getCustomNotificationLayout() {
return R.layout.exo_notification_layout;
}
PlayerNotificationManager
与您的ExoPlayer
实例关联:playerNotificationManager.setPlayer(exoPlayer);
现在,ExoPlayer通知将不再显示快退和快进按钮。如果需要,您可以根据需要自定义播放控制按钮或其他控件。
领取专属 10元无门槛券
手把手带您无忧上云