Skip to content

Commit

Permalink
AlertPlayer: safely handle stopping sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Apr 12, 2022
1 parent b5a2e88 commit da6bb4f
Showing 1 changed file with 7 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});

Expand Down

0 comments on commit da6bb4f

Please sign in to comment.