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

View File

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