java - ExoPlayer:不推荐使用 setNotificationListener
问题描述
Exoplayer 的最新更新com.google.android.exoplayer:exoplayer:2.10.1
我注意到该setNotificationListener
方法现在已弃用,文档说:
@param notificationListener {@link NotificationListener}。@deprecated 将通知侦听器传递给构造函数。
我不明白,我应该把监听器放在哪里的构造函数
void exoPlayerNotification(Context context, SimpleExoPlayer exoPlayer, String title) {
String titlesonge;
String artist;
try {
titlesonge = StringUtils.substringBefore(title, " - ");
artist = StringUtils.substringAfter(title, " - ");
} catch (Exception e) {
titlesonge = title.substring(0, title.indexOf(" - "));
artist = title.substring(title.lastIndexOf(" - ") - 1);
}
String finalArtist = artist;
String finalTitlesonge = titlesonge;
PlayerNotificationManager mPlayerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
context,
"PRIMARY_CHANNEL_ID",
R.string.plaza,
NOTIFICATION_ID,
new PlayerNotificationManager.MediaDescriptionAdapter() {
@Override
public String getCurrentContentTitle(Player player) {
return finalArtist;
}
@Nullable
@Override
public PendingIntent createCurrentContentIntent(Player player) {
Intent intent = new Intent(service, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
return PendingIntent.getActivity(service, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
@Override
public String getCurrentContentText(Player player) {
return finalTitlesonge;
}
@Nullable
@Override
public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
return BitmapFactory.decodeResource(service.getResources(), R.drawable.largeicon);
}
@Nullable
@Override
public String getCurrentSubText(Player player) {
return null;
}
}
);
mPlayerNotificationManager.setUseNavigationActions(false);
mPlayerNotificationManager.setFastForwardIncrementMs(0);
mPlayerNotificationManager.setRewindIncrementMs(0);
mPlayerNotificationManager.setColorized(true);
mPlayerNotificationManager.setColor(0xFFBDBDBD);
mPlayerNotificationManager.setUseChronometer(true);
mPlayerNotificationManager.setPriority(NotificationCompat.PRIORITY_MAX);
mPlayerNotificationManager.setUsePlayPauseActions(true);
mPlayerNotificationManager.setUseStopAction(true);
mPlayerNotificationManager.setSmallIcon(R.drawable.smallwidth);
mPlayerNotificationManager.setNotificationListener(new PlayerNotificationManager.NotificationListener() {
@Override
public void onNotificationStarted(int notificationId, Notification notification) {
service.startForeground(notificationId, notification);
mPlayerNotificationManager.setColorized(true);
}
@Override
public void onNotificationCancelled(int notificationId) {
service.stopSelf();
cancelNotify();
}
@Override
public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
if (dismissedByUser)
exoPlayer.setPlayWhenReady(false);
}
@Override
public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
}
});
mPlayerNotificationManager.setPlayer(exoPlayer);
}
解决方案
我也花了很长时间才发现这一点..
不要在创建 PlayerNotificationManager 后调用,而是在 mPlayerNotificationManager 构造函数(作为第 6 个参数)中.setNotificationListener()
使用NotificationListener 对象,如下所示:
PlayerNotificationManager mPlayerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
context,
"PRIMARY_CHANNEL_ID",
R.string.plaza,
NOTIFICATION_ID,
new PlayerNotificationManager.MediaDescriptionAdapter() {...},
new PlayerNotificationManager.NotificationListener() {
// Implement your methods here again
}
);
推荐阅读
- html - 为什么我无法使用 MSXML2 和 VBA 将 HTML 类名添加到元素集合
- scala - 加特林 gRPC 身份验证
- c++ - 如何在面向对象语言中打开子类型?
- android - 从 Guice DI 转换为 Hilt/Dagger DI 时是否需要遵循任何特定规则
- java - 将非规范化或平面数据转换为分层 Java 对象
- bash - 调用 bash 时 Perl 脚本运行缓慢
- css - 如何在第二个 Tailwind CSS 网格列中左对齐内容,以便在所有屏幕尺寸下保持它与第一列之间的间隙?
- azure-pipelines-build-task - 尝试保存包含自定义任务的管道时出错
- python - 从 pandas 数据框创建边缘列表
- python - python with pandas 解析日期,如“0001-11-29 13:00:00 BC”