Fix start/stop audio race condition.

Fixes #4289
// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-10-23 16:54:58 -07:00
parent 9dc003a753
commit 2ce16297ff

View File

@ -72,14 +72,19 @@ public class AudioSlidePlayer {
@Override @Override
public void onPrepared(MediaPlayer mp) { public void onPrepared(MediaPlayer mp) {
Log.w(TAG, "onPrepared"); Log.w(TAG, "onPrepared");
if (progress > 0) { synchronized (AudioSlidePlayer.this) {
mediaPlayer.seekTo((int)(mediaPlayer.getDuration() * progress)); if (mediaPlayer == null) return;
if (progress > 0) {
mediaPlayer.seekTo((int) (mediaPlayer.getDuration() * progress));
}
mediaPlayer.start();
setPlaying(AudioSlidePlayer.this);
} }
mediaPlayer.start();
notifyOnStart(); notifyOnStart();
setPlaying(AudioSlidePlayer.this);
progressEventHandler.sendEmptyMessage(0); progressEventHandler.sendEmptyMessage(0);
} }
}); });
@ -88,9 +93,11 @@ public class AudioSlidePlayer {
@Override @Override
public void onCompletion(MediaPlayer mp) { public void onCompletion(MediaPlayer mp) {
Log.w(TAG, "onComplete"); Log.w(TAG, "onComplete");
mediaPlayer = null; synchronized (AudioSlidePlayer.this) {
audioAttachmentServer.stop(); mediaPlayer = null;
audioAttachmentServer = null; audioAttachmentServer.stop();
audioAttachmentServer = null;
}
notifyOnStop(); notifyOnStop();
progressEventHandler.removeMessages(0); progressEventHandler.removeMessages(0);
@ -109,24 +116,9 @@ public class AudioSlidePlayer {
mediaPlayer.prepareAsync(); mediaPlayer.prepareAsync();
} }
public void stop() { public synchronized void stop() {
Log.w(TAG, "Stop called!"); 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); removePlaying(this);
if (this.mediaPlayer != null) { if (this.mediaPlayer != null) {
@ -141,6 +133,18 @@ public class AudioSlidePlayer {
this.audioAttachmentServer = null; 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<Double, Integer> getProgress() { private Pair<Double, Integer> getProgress() {
if (mediaPlayer == null || mediaPlayer.getCurrentPosition() <= 0 || mediaPlayer.getDuration() <= 0) { if (mediaPlayer == null || mediaPlayer.getCurrentPosition() <= 0 || mediaPlayer.getDuration() <= 0) {
return new Pair<>(0D, 0); return new Pair<>(0D, 0);