有没有人有办法解决吗?
Fatal Exception: java.lang.IllegalStateException: finishBroadcast() called outside of a broadcast
at android.os.RemoteCallbackList.finishBroadcast(RemoteCallbackList.java:303)
at android.support.v4.media.session.MediaSessionCompat$MediaSessionImplApi21.setCallback(Unknown Source:28)
at android.support.v4.media.session.MediaSessionCompat.setCallback(Unknown Source:2)
at com.test.player.myService.onCurrentPlayerChanged(Unknown Source:196)
at com.test.player.myService.onCurrentPlayerChanged(Unknown Source:53)
at com.test.player.myService$$Lambda$3.accept(Unknown Source:6)
at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onNext(Unknown Source:7)
at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onNext(Unknown Source:12)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onSubscribe(Unknown Source:23)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onSubscribe(Unknown Source:6)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onNext(Unknown Source:51)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onSubscribe(Unknown Source:23)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onSubscribe(Unknown Source:6)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onNext(Unknown Source:51)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(Unknown Source:47)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(Unknown Source:8)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(Unknown Source:13)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(Unknown Source)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
此堆栈跟踪来自 Fabric crashlytics。
onCurrentPlayerChanged()
只需拨打stopForeground
服务电话。
public void onCurrentPlayerChanged(int currentPlayer) {
if (currentPlayer != LuxePreferences.PLAYER_ONE) {
stopForeground(true);
NotificationManagerCompat.from(this).cancel(PLAYER_NOTIFICATION_ID);
}
}
这是 onCreate() 和 onStartCommand()
public void onCreate() {
super.onCreate();
// Create a MediaSessionCompat
mediaSession = new MediaSessionCompat(getApplicationContext(), "RadioService",
new ComponentName(getApplicationContext(), RadioMediaButtonReceiver.class), null);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
stateBuilder = new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_STOPPED, 0, 1)
.setActions(
PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_PAUSE);
mediaSession.setPlaybackState(stateBuilder.build());
mediaSession.setCallback(mediaSessionCallback);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (MediaButtonReceiver.handleIntent(mediaSession, intent) == null) {
int action = intent == null ? ACTION_NONE : intent.getIntExtra(EXTRA_ACTION, ACTION_NONE);
if (action == ACTION_NONE) {
if (preferences.isRadioOn()) {
startRadio();
} else {
boolean removeNotificationOnStop =
intent != null && intent.getBooleanExtra(EXTRA_REMOVE_NOTIFICATION_ON_STOP, false);
stopRadio(removeNotificationOnStop);
}
} else if (action == ACTION_STOP) {
stopRadio(true);
stopSelf();
}
}
return Service.START_STICKY;
}
在 startRadio() 上,我mediaSession.setActive(true);
也在 stopRadio() 中设置mediaSession.setActive(false)
在设置通知时设置 mediaSession 播放状态如下
if (isPlaying()) {
mediaSession.setPlaybackState(stateBuilder
.setState(PlaybackStateCompat.STATE_PLAYING, 0, 1).build());
startForeground(PLAYER_NOTIFICATION_ID, notification);
} else {
mediaSession.setPlaybackState(stateBuilder
.setState(PlaybackStateCompat.STATE_PAUSED, 0, 0).build());
stopForeground(false);
}
和 onDestroy()
@Override
public void onDestroy() {
super.onDestroy();
//other stuff to release resources
if (mediaSession != null) {
mediaSession.release();
}
}
关于这个异常的原因的任何想法?