java - Exo播放器 : setNotificationListener is deprecated

标签 java android android-notifications exoplayer2.x

Exoplayer 的最新更新 com.google.android.exoplayer:exoplayer:2.10.1 我注意到 setNotificationListener 方法现已弃用,文档称:

@param notificationListener The {@link NotificationListener}. @deprecated Pass the notification listener to the constructor instead.

我不明白,我应该在上面放置监听器的构造函数在哪里

 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 后调用 .setNotificationListener(),而是在 mPlayerNotificationManager 构造函数中使用 NotificationListener 对象(作为第 6 个参数),如下所示:

    PlayerNotificationManager mPlayerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
            context,
            "PRIMARY_CHANNEL_ID",
            R.string.plaza,
            NOTIFICATION_ID,
            new PlayerNotificationManager.MediaDescriptionAdapter() {...},
            new PlayerNotificationManager.NotificationListener() {
                     // Implement your methods here again
            }
    );

关于java - Exo播放器 : setNotificationListener is deprecated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56499756/

相关文章:

java - 将 HTTP GET 请求中的 JSON 数据从 JAVA 代码发送到 REST API

java - 如何在jsf中显示多对多的jpa集合?

android - 显示通知时华为 5.1 手机出现奇怪的允许/拒绝问题

java - 将 ListView 点击值传递给具有共享首选项的另一个 Activity

Java 数据包困惑

android - Android 10 中的应用程序图标有时是错误的

Android ACTION_GET_CONTENT 不更新下载目录文件

android - 不能使用类型 T 作为具体化参数,请使用类代替

java - 使用 Java Drawable 作为通知图标

android - 无法在 android studio 3.4.1 中解析符号 'NotificationCompat '