From b5a9f042cc32ec4a3011802cebdd13ceb108be86 Mon Sep 17 00:00:00 2001 From: John Zhen M Date: Tue, 10 Oct 2017 19:32:47 -0700 Subject: [PATCH] -Fixed background player activity crashes on receiving update when stopped (lifecycle still active). --- .../newpipe/player/BackgroundPlayer.java | 8 +++++- .../player/BackgroundPlayerActivity.java | 28 +++++++++++-------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java index c48de1ed4..d4e2b0dbe 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java @@ -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()); } } diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayerActivity.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayerActivity.java index 05ba67cdd..2087f943d 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayerActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayerActivity.java @@ -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) {