From cdcfa8c28e22652d488d7c71c36579557c410da7 Mon Sep 17 00:00:00 2001 From: Olivier Bouillet Date: Fri, 25 Sep 2020 21:56:09 +0200 Subject: [PATCH] [Crash] : avoid crash when leaving the application --- .../peertube/service/VideoPlayerService.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java b/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java index 96e71b1..94414d7 100644 --- a/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java +++ b/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java @@ -113,8 +113,7 @@ public class VideoPlayerService extends Service { if (playbackState == ACTION_PLAY) { // this means that play is available, hence the audio is paused or stopped Log.v(TAG, "ACTION_PAUSE: " + playbackState); - unregisterReceiver(myNoisyAudioStreamReceiver); - myNoisyAudioStreamReceiver = null; + safeUnregisterReceiver(); } } }); @@ -137,14 +136,9 @@ public class VideoPlayerService extends Service { if (playerNotificationManager != null) { playerNotificationManager.setPlayer(null); } - //Was seeing an error when exiting the program about about not unregistering the receiver. - try { - if (null != myNoisyAudioStreamReceiver) { - this.unregisterReceiver(myNoisyAudioStreamReceiver); - } - } catch (Exception e) { - Log.e("VideoPlayerService", "attempted to unregister a nonregistered service"); - } + //Was seeing an error when exiting the program about not unregistering the receiver. + safeUnregisterReceiver(); + if (player != null) { player.release(); player = null; @@ -152,6 +146,15 @@ public class VideoPlayerService extends Service { super.onDestroy(); } + private void safeUnregisterReceiver() + { + try { + unregisterReceiver(myNoisyAudioStreamReceiver); + } catch (Exception e) { + Log.e("VideoPlayerService", "attempted to unregister a nonregistered service"); + } + } + @Nullable @Override public IBinder onBind(Intent intent) {