From 2ce16297ff1dfbbc948aec1c5d352c46f95c2547 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Fri, 23 Oct 2015 16:54:58 -0700 Subject: [PATCH] Fix start/stop audio race condition. Fixes #4289 // FREEBIE --- .../securesms/audio/AudioSlidePlayer.java | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/src/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java b/src/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java index bb0dab829f..416166c145 100644 --- a/src/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java +++ b/src/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java @@ -72,14 +72,19 @@ public class AudioSlidePlayer { @Override public void onPrepared(MediaPlayer mp) { Log.w(TAG, "onPrepared"); - if (progress > 0) { - mediaPlayer.seekTo((int)(mediaPlayer.getDuration() * progress)); + synchronized (AudioSlidePlayer.this) { + if (mediaPlayer == null) return; + + if (progress > 0) { + mediaPlayer.seekTo((int) (mediaPlayer.getDuration() * progress)); + } + + mediaPlayer.start(); + + setPlaying(AudioSlidePlayer.this); } - mediaPlayer.start(); - notifyOnStart(); - setPlaying(AudioSlidePlayer.this); progressEventHandler.sendEmptyMessage(0); } }); @@ -88,9 +93,11 @@ public class AudioSlidePlayer { @Override public void onCompletion(MediaPlayer mp) { Log.w(TAG, "onComplete"); - mediaPlayer = null; - audioAttachmentServer.stop(); - audioAttachmentServer = null; + synchronized (AudioSlidePlayer.this) { + mediaPlayer = null; + audioAttachmentServer.stop(); + audioAttachmentServer = null; + } notifyOnStop(); progressEventHandler.removeMessages(0); @@ -109,24 +116,9 @@ public class AudioSlidePlayer { mediaPlayer.prepareAsync(); } - public void stop() { + public synchronized void stop() { Log.w(TAG, "Stop called!"); - shutdown(); - } - public void setListener(@NonNull Listener listener) { - this.listener = new WeakReference<>(listener); - - if (this.mediaPlayer != null && this.mediaPlayer.isPlaying()) { - notifyOnStart(); - } - } - - public @NonNull AudioSlide getAudioSlide() { - return slide; - } - - private void shutdown() { removePlaying(this); if (this.mediaPlayer != null) { @@ -141,6 +133,18 @@ public class AudioSlidePlayer { this.audioAttachmentServer = null; } + public void setListener(@NonNull Listener listener) { + this.listener = new WeakReference<>(listener); + + if (this.mediaPlayer != null && this.mediaPlayer.isPlaying()) { + notifyOnStart(); + } + } + + public @NonNull AudioSlide getAudioSlide() { + return slide; + } + private Pair getProgress() { if (mediaPlayer == null || mediaPlayer.getCurrentPosition() <= 0 || mediaPlayer.getDuration() <= 0) { return new Pair<>(0D, 0);