cutykaryspace
cutykaryspace

Reputation: 51

Attempt to invoke interface method 'android.media.session.ISessionController android.media.session.ISession.getController()' on a null object

Below code is for music notification player with controls using media session.Whenever I click controls from notification it crashes with error mentioned above. Please see the code below and correct me if I am wrong.In below code I have used media session and broadcast receiver to build the notification. "track" is my model class for all the song data.

   try {
                if (track.getImage() == null) {
                    track.setImage(BitmapFactory.decodeResource(context.getResources(),
                            R.mipmap.ic_launcher));
                }
    
    
                NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
                MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(context, "tag");
                mediaSessionCompat.setMetadata(
                        new MediaMetadataCompat.Builder()
                                .putString(MediaMetadata.METADATA_KEY_TITLE, track.getTitle())
                                .putString(MediaMetadata.METADATA_KEY_ARTIST, track.getArtist())
                                .putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, track.getImage())
                                .putString(MediaMetadata.METADATA_KEY_ALBUM, track.getAlbum())
                                .build()
                );
                mediaSessionCompat.setFlags(
                        MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                                MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    
                PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder();
    
                stateBuilder.setActiveQueueItemId(MediaSession.QueueItem.UNKNOWN_ID);
    
                long actions = PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
                stateBuilder.setActions(actions);
                if (isPlaying) {
                    stateBuilder.setState(PlaybackStateCompat.STATE_PLAYING, 0, 1.0f);
                    mediaSessionCompat.setActive(true);
    
                } else {
                    stateBuilder.setState(PlaybackStateCompat.STATE_PAUSED, 0, 1.0f);
                    mediaSessionCompat.setActive(true);
    
    
                }
                mediaSessionCompat.setPlaybackState(stateBuilder.build());
                Bitmap icon = track.getImage();
    
    
                PendingIntent pendingIntentPrevious;
                int drw_previous;
    
    //                pendingIntentPrevious = null;
    //                drw_previous = 0;
                Intent intentPrevious = new Intent(context, NotificationActionService.class)
                        .setAction(ACTION_PREVIUOS);
                pendingIntentPrevious = PendingIntent.getBroadcast(context, 0,
                        intentPrevious, PendingIntent.FLAG_UPDATE_CURRENT);
                drw_previous = R.drawable.ic_back;
                Intent intentPlay = new Intent(context, NotificationActionService.class)
                        .setAction(ACTION_PLAY);
                PendingIntent pendingIntentPlay = PendingIntent.getBroadcast(context, 0,
                        intentPlay, PendingIntent.FLAG_UPDATE_CURRENT);
    
                PendingIntent pendingIntentNext;
                int drw_next;
    //                pendingIntentNext = null;
    //                drw_next = 0;
                Intent intentNext = new Intent(context, NotificationActionService.class)
                        .setAction(ACTION_NEXT);
                pendingIntentNext = PendingIntent.getBroadcast(context, 0,
                        intentNext, PendingIntent.FLAG_UPDATE_CURRENT);
                drw_next = R.drawable.ic_next;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    notification = new NotificationCompat.Builder(context, CHANNEL_ID)
                            .setSmallIcon(R.drawable.logo)
                            .setContentTitle(track.getTitle())
                            .setContentText(track.getArtist())
                            .setLargeIcon(icon)
                            .setAutoCancel(isPlaying ? false : true)
                            .setOngoing(isPlaying ? true : false)
                            .setWhen(0)
                            .setNotificationSilent()
                            .setSound(null)
                            .addAction(drw_previous, "Previous", pendingIntentPrevious)
                            .addAction(playbutton, "Play", pendingIntentPlay)
                            .addAction(drw_next, "Next", pendingIntentNext)
                            .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                                    .setShowActionsInCompactView(0, 1, 2)
                                    .setMediaSession(mediaSessionCompat.getSessionToken()))
                            .setPriority(NotificationCompat.PRIORITY_LOW)
                            .build();
    
                } else {
                    notification = new NotificationCompat.Builder(context, CHANNEL_ID)
                            .setSmallIcon(R.drawable.logo)
                            .setContentTitle(track.getTitle())
                            .setContentText(track.getArtist())
                            .setLargeIcon(icon)
                            .setAutoCancel(true)
                            .setOngoing(false)
                            .setWhen(0)
                            .setNotificationSilent()
                            .setSound(null)
                            .addAction(drw_previous, "Previous", pendingIntentPrevious)
                            .addAction(playbutton, "Play", pendingIntentPlay)
                            .addAction(drw_next, "Next", pendingIntentNext)
                            .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                                    .setShowActionsInCompactView(0, 1, 2)
                                    .setMediaSession(mediaSessionCompat.getSessionToken()))
                            .setPriority(NotificationCompat.PRIORITY_LOW)
                            .build();
    
                }
    
                notificationManagerCompat.notify(1, notification);
            } catch (Exception e) {
                Log.e("media sesison", e.getLocalizedMessage());
            }

   

Upvotes: 5

Views: 1915

Answers (2)

Nikolay Krasilnikov
Nikolay Krasilnikov

Reputation: 1020

There is an internal Android MediaSessions limit SESSION_CREATION_LIMIT_PER_UID = 100;

You should release MediaSession instances that you don't need anymore.

How to reproduce:

// Just create 100 instances of MediaSession
repeat(200) {
    val session = MediaSessionCompat(context, "Session") // Will cause a crash after 99 iterations
}

How to fix:

// You should release MediaSession instances that you don't need anymore.
repeat(200) {
    val session = MediaSessionCompat(context, "Session")
    session.release()
}

Upvotes: 6

YUYI-CHEN
YUYI-CHEN

Reputation: 1

I encountered the same problem and I solved it by the following method.

you should call setActive(true) when you create the session and release it when destroyed.

Upvotes: 0

Related Questions