From da6bb4f7fa9dc55e339000fb00209a5b0fc7dfd7 Mon Sep 17 00:00:00 2001 From: Jamorham Date: Tue, 12 Apr 2022 15:23:53 +0000 Subject: [PATCH] AlertPlayer: safely handle stopping sounds --- .../dexdrip/UtilityModels/AlertPlayer.java | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/AlertPlayer.java b/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/AlertPlayer.java index eee08eb54c..01902fbeba 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/AlertPlayer.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/UtilityModels/AlertPlayer.java @@ -40,6 +40,8 @@ import java.util.Date; import static com.eveningoutpost.dexdrip.Home.startWatchUpdaterService; +import static com.eveningoutpost.dexdrip.Models.JoH.delayedMediaPlayerRelease; +import static com.eveningoutpost.dexdrip.Models.JoH.stopAndReleasePlayer; // A helper class to create the mediaplayer on the UI thread. // This is needed in order for the callbackst to work. @@ -169,14 +171,7 @@ public synchronized void stopAlert(Context ctx, boolean ClearData, boolean clear notificationDismiss(ctx); } if (mediaPlayer != null) { - if (mediaPlayer.isPlaying()) { - try { - mediaPlayer.stop(); - } catch (IllegalStateException e) { - UserError.Log.e(TAG, "Exception when stopping media player: " + e); - } - } - mediaPlayer.release(); + stopAndReleasePlayer(mediaPlayer); mediaPlayer = null; } revertCurrentVolume(streamType); @@ -325,9 +320,8 @@ protected synchronized void playFile(final Context ctx, final String fileName, f } if (mediaPlayer != null) { - Log.i(TAG, "ERROR, playFile:going to leak a mediaplayer !!!"); - mediaPlayer.release(); - mediaPlayer = null; + Log.i(TAG, "ERROR, playFile sound already playing"); + stopAndReleasePlayer(mediaPlayer); } mediaPlayer = new MediaPlayerCreaterHelper().createMediaPlayer(ctx); @@ -338,8 +332,8 @@ protected synchronized void playFile(final Context ctx, final String fileName, f mediaPlayer.setOnCompletionListener(mp -> { Log.i(TAG, "playFile: onCompletion called (finished playing) "); - mediaPlayer.release(); - mediaPlayer = null; + delayedMediaPlayerRelease(mp); + JoH.threadSleep(300); revertCurrentVolume(streamType); });