-Fixed background player activity crashes on receiving update when stopped (lifecycle still active).

This commit is contained in:
John Zhen M 2017-10-10 19:32:47 -07:00 committed by John Zhen Mo
parent 770dcc1832
commit b5a9f042cc
2 changed files with 23 additions and 13 deletions

View File

@ -484,6 +484,12 @@ public final class BackgroundPlayer extends Service {
triggerProgressUpdate(); triggerProgressUpdate();
} }
public void removeActivityListener(PlayerEventListener listener) {
if (activityListener == listener) {
activityListener = null;
}
}
private void updateMetadata() { private void updateMetadata() {
if (activityListener != null && currentInfo != null) { if (activityListener != null && currentInfo != null) {
activityListener.onMetadataUpdate(currentInfo); activityListener.onMetadataUpdate(currentInfo);
@ -491,7 +497,7 @@ public final class BackgroundPlayer extends Service {
} }
private void updatePlayback() { private void updatePlayback() {
if (activityListener != null) { if (activityListener != null && simpleExoPlayer != null && playQueue != null) {
activityListener.onPlaybackUpdate(currentState, simpleExoPlayer.getRepeatMode(), playQueue.isShuffled(), simpleExoPlayer.getPlaybackParameters()); activityListener.onPlaybackUpdate(currentState, simpleExoPlayer.getRepeatMode(), playQueue.isShuffled(), simpleExoPlayer.getPlaybackParameters());
} }
} }

View File

@ -98,11 +98,6 @@ public class BackgroundPlayerActivity extends AppCompatActivity
} }
serviceConnection = backgroundPlayerConnection(); serviceConnection = backgroundPlayerConnection();
}
@Override
protected void onStart() {
super.onStart();
bind(); bind();
} }
@ -121,8 +116,8 @@ public class BackgroundPlayerActivity extends AppCompatActivity
} }
@Override @Override
protected void onStop() { protected void onDestroy() {
super.onStop(); super.onDestroy();
unbind(); unbind();
} }
@ -143,6 +138,7 @@ public class BackgroundPlayerActivity extends AppCompatActivity
if(serviceBound) { if(serviceBound) {
unbindService(serviceConnection); unbindService(serviceConnection);
serviceBound = false; serviceBound = false;
stopPlayerListener();
player = null; player = null;
finish(); finish();
} }
@ -164,6 +160,7 @@ public class BackgroundPlayerActivity extends AppCompatActivity
unbind(); unbind();
} else { } else {
buildComponents(); buildComponents();
startPlayerListener();
} }
} }
}; };
@ -178,7 +175,6 @@ public class BackgroundPlayerActivity extends AppCompatActivity
buildMetadata(); buildMetadata();
buildSeekBar(); buildSeekBar();
buildControls(); buildControls();
buildListeners();
} }
private void buildQueue() { private void buildQueue() {
@ -230,10 +226,6 @@ public class BackgroundPlayerActivity extends AppCompatActivity
buildPlaybackPitchMenu(); buildPlaybackPitchMenu();
} }
private void buildListeners() {
player.setActivityListener(this);
}
private void buildPlaybackSpeedMenu() { private void buildPlaybackSpeedMenu() {
if (playbackSpeedPopupMenu == null) return; if (playbackSpeedPopupMenu == null) return;
@ -424,6 +416,18 @@ public class BackgroundPlayerActivity extends AppCompatActivity
// Binding Service Listener // Binding Service Listener
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
private void startPlayerListener() {
if (player != null) {
player.setActivityListener(this);
}
}
private void stopPlayerListener() {
if (player != null) {
player.removeActivityListener(this);
}
}
@Override @Override
public void onPlaybackUpdate(int state, int repeatMode, boolean shuffled, PlaybackParameters parameters) { public void onPlaybackUpdate(int state, int repeatMode, boolean shuffled, PlaybackParameters parameters) {
switch (state) { switch (state) {