Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExoPlayer : setNotificationListener is deprecated

the latest update of Exoplayer com.google.android.exoplayer:exoplayer:2.10.1 I noticed that the setNotificationListener method is now deprecated, documentation said :

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

I don't get it, where is the constructor I should put the listener on it

 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);
}
like image 760
Mouaad Abdelghafour AITALI Avatar asked Dec 17 '22 17:12

Mouaad Abdelghafour AITALI


2 Answers

I took me waay to long to find this out as well..

Instead of calling .setNotificationListener() after creation of PlayerNotificationManager, use a NotificationListener object in the mPlayerNotificationManager constructor (as a 6th argument) like so:

    PlayerNotificationManager mPlayerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
            context,
            "PRIMARY_CHANNEL_ID",
            R.string.plaza,
            NOTIFICATION_ID,
            new PlayerNotificationManager.MediaDescriptionAdapter() {...},
            new PlayerNotificationManager.NotificationListener() {
                     // Implement your methods here again
            }
    );
like image 123
Erwin van den Berg Avatar answered Dec 21 '22 11:12

Erwin van den Berg


You have to override the correct method of onNotificationPosted

playerNotificationManager.setNotificationListener(new PlayerNotificationManager.NotificationListener() {
        @Override
        public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
            startForeground(notificationId,notification);
        }

        @Override
        public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
         stopSelf();
        }
    });
like image 40
Shubam Virdi Avatar answered Dec 21 '22 09:12

Shubam Virdi