First attempt to display initial duration for audio attachements.

This commit is contained in:
chklump
2019-02-02 19:21:39 +01:00
parent 44075917b7
commit ea20bf20c6
2 changed files with 53 additions and 0 deletions
@@ -78,6 +78,48 @@ public class AudioSlidePlayer implements SensorEventListener {
}
}
public void requestDuration() {
if(this.mediaPlayer == null) {
this.mediaPlayer = new MediaPlayerWrapper();
try {
this.audioAttachmentServer = new AttachmentServer(context, slide.asAttachment());
this.startTime = System.currentTimeMillis();
audioAttachmentServer.start();
this.mediaPlayer.setDataSource(context, audioAttachmentServer.getUri());
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.w(TAG, "onPrepared");
synchronized (AudioSlidePlayer.this) {
if (mediaPlayer == null) return;
}
getListener().onReceivedDuration(mediaPlayer.getDuration());
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
if (audioAttachmentServer != null) {
audioAttachmentServer.stop();
}
mediaPlayer = null;
audioAttachmentServer = null;
}
});
mediaPlayer.prepareAsync();
} catch (IOException e) {
Log.w(TAG, e);
getListener().onReceivedDuration(0);
}
} else {
getListener().onReceivedDuration(0);
}
}
public void play(final double progress) throws IOException {
play(progress, false);
}
@@ -256,6 +298,8 @@ public class AudioSlidePlayer implements SensorEventListener {
public void onStop() {}
@Override
public void onProgress(double progress, long millis) {}
@Override
public void onReceivedDuration(long millis) {}
};
}
@@ -321,6 +365,7 @@ public class AudioSlidePlayer implements SensorEventListener {
public void onStart();
public void onStop();
public void onProgress(double progress, long millis);
public void onReceivedDuration(long millis);
}
private static class ProgressEventHandler extends Handler {
@@ -90,6 +90,7 @@ public class AudioView extends FrameLayout implements AudioSlidePlayer.Listener
controlToggle.displayQuick(playButton);
seekBar.setEnabled(true);
this.audioSlidePlayer = AudioSlidePlayer.createFor(getContext(), audio, this);
this.audioSlidePlayer.requestDuration();
}
public void cleanup() {
@@ -98,6 +99,13 @@ public class AudioView extends FrameLayout implements AudioSlidePlayer.Listener
}
}
@Override
public void onReceivedDuration(long millis) {
this.timestamp.setText(String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis)));
}
@Override
public void onStart() {
if (this.pauseButton.getVisibility() != View.VISIBLE) {