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
public void onPrepared(MediaPlayer mp) {
Log.w(TAG, "onPrepared");
synchronized (AudioSlidePlayer.this) {
if (mediaPlayer == null) return;
if (progress > 0) {
mediaPlayer.seekTo((int)(mediaPlayer.getDuration() * progress));
mediaPlayer.seekTo((int) (mediaPlayer.getDuration() * progress));
}
mediaPlayer.start();
notifyOnStart();
setPlaying(AudioSlidePlayer.this);
}
notifyOnStart();
progressEventHandler.sendEmptyMessage(0);
}
});
@ -88,9 +93,11 @@ public class AudioSlidePlayer {
@Override
public void onCompletion(MediaPlayer mp) {
Log.w(TAG, "onComplete");
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<Double, Integer> getProgress() {
if (mediaPlayer == null || mediaPlayer.getCurrentPosition() <= 0 || mediaPlayer.getDuration() <= 0) {
return new Pair<>(0D, 0);