首页 > 解决方案 > 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);
}

标签: javaandroidandroid-notificationsexoplayer2.x

解决方案


我也花了很长时间才发现这一点..

不要在创建 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
            }
    );

推荐阅读