Мне не удается заставить работать мультимедийную кнопку уведомления.
У меня появляется эта ошибка, когда я нажимаю на игру:
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();
}
}
А в моем андроиде манифест:
<receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
Я не знаю, почему метод onBind моего MusicPlayer вызывается, когда я нажимаю кнопку мультимедиа!
Спасибо!