2

我无法使通知的媒体按钮工作。单击播放时出现此错误:

     java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                            at android.os.Binder.queryLocalInterface(Binder.java:435)
                                                                            at android.service.media.IMediaBrowserService$Stub.asInterface(IMediaBrowserService.java:31)
                                                                            at android.media.browse.MediaBrowser$MediaServiceConnection$1.run(MediaBrowser.java:990)
                                                                            at android.media.browse.MediaBrowser$MediaServiceConnection.postOrRun(MediaBrowser.java:1051)
                                                                            at android.media.browse.MediaBrowser$MediaServiceConnection.onServiceConnected(MediaBrowser.java:974)
                                                                            at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1691)
                                                                            at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1720)
                                                                            at android.os.Handler.handleCallback(Handler.java:790)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                            at android.os.Looper.loop(Looper.java:164)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6753)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

这是我的通知类(你可以看到我使用了 buildMediaButtonPendingIntent 方法):

  public MediaNotificationBuilder(Service context, MediaSessionCompat mediaSession) {
        super(context);
        this.mContext = context;
        this.mediaSession = mediaSession;
        notificationManager = NotificationManagerCompat.from(context);
        initializeCallbacksAndActions();
        initializeNotification();
    }

    private void initializeCallbacksAndActions() {
        mediaSession.getController().registerCallback(new MediaControllerCompat.Callback() {
            @Override
            public void onMetadataChanged(MediaMetadataCompat metadata) {
                super.onMetadataChanged(metadata);
                initializeNotification();
            }

            @Override
            public void onPlaybackStateChanged(PlaybackStateCompat state) {
                super.onPlaybackStateChanged(state);
                initializeNotification();
            }
        });

        skipToPreviousAction = new NotificationCompat.Action(
                R.drawable.ic_notification_previous,
                getContext().getString(R.string.notification_skip_to_previous),
                MediaButtonReceiver.buildMediaButtonPendingIntent(mContext, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
        );
        skipToNextAction = new NotificationCompat.Action(
                R.drawable.ic_notificication_next,
                mContext.getString(R.string.notification_skip_to_next),
                MediaButtonReceiver.buildMediaButtonPendingIntent(mContext, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)
        );
        playAction = new NotificationCompat.Action(
                R.drawable.ic_notification_play,
                getContext().getString(R.string.notification_play),
                MediaButtonReceiver.buildMediaButtonPendingIntent(mContext, PlaybackStateCompat.ACTION_PLAY)
        );
        pauseAction = new NotificationCompat.Action(
                R.drawable.ic_notification_pause,
                getContext().getString(R.string.notification_play),
                MediaButtonReceiver.buildMediaButtonPendingIntent(mContext, PlaybackStateCompat.ACTION_PAUSE)
        );

        PendingIntent pause = MediaButtonReceiver.buildMediaButtonPendingIntent(mContext, PlaybackStateCompat.ACTION_PAUSE);

        builder = new NotificationCompat.Builder(getContext(), CHANNEL_ID);

        //Customize the notification
        builder
                .setShowWhen(false)
                .setUsesChronometer(false)
                .setCategory(Notification.CATEGORY_SERVICE)
                .setSmallIcon(R.drawable.ic_player_notification)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    }

    private void initializeNotification() {
        try {
            createNotificationChannel(CHANNEL_ID);
            initializePlaybackState();
            initializeMetadata();
            mContext.startForeground(NOTIFICATION_ID,builder.build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

在我的 android 清单中:

    <receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON"/>
        </intent-filter>
    </receiver>

我不知道为什么当我按下媒体按钮时会调用我的 MusicPlayer 的 onBind 方法!谢谢 !

4

0 回答 0