尝试在空对象上调用接口方法 'android.media.session.ISessionController android.media.session.ISession.getController()'

问题描述

下面的代码适用于带有使用媒体会话的控件的音乐通知播放器。每当我单击通知中的控件时,它都会因上述错误而崩溃。如果我错了,请查看下面的代码并纠正我。在下面的代码中,我使用了媒体会话和广播接收器来构建通知。 “track”是我所有歌曲数据的模型类。

   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,1.0f);
                    mediaSessionCompat.setActive(true);
    
                } else {
                    stateBuilder.setState(PlaybackStateCompat.STATE_PAUSED,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,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,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,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,2)
                                    .setMediaSession(mediaSessionCompat.getSessionToken()))
                            .setPriority(NotificationCompat.PRIORITY_LOW)
                            .build();
    
                }
    
                notificationmanagerCompat.notify(1,notification);
            } catch (Exception e) {
                Log.e("media sesison",e.getLocalizedMessage());
            }

   

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)