Skip to content

Commit

Permalink
Smooth out audio controller seek bar
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Oct 19, 2020
1 parent a9c6add commit 0442050
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.odk.collect.async.Cancellable
import org.odk.collect.async.Scheduler
import java.io.File
import java.io.IOException
import java.lang.Integer.max
import java.util.*
import java.util.function.Supplier
import kotlin.jvm.Throws
Expand Down Expand Up @@ -148,13 +149,16 @@ class AudioClipViewModel(private val mediaPlayerFactory: Supplier<MediaPlayer>,
}

private fun schedulePositionUpdates() {
val clipDuration = mediaPlayer.duration
val timeBetweenUpdates = max(clipDuration / 100, 1000 / 12) // Never faster than 12fps

positionUpdatesCancellable = scheduler.repeat(Runnable {
val currentlyPlaying = currentlyPlaying.value
if (currentlyPlaying != null) {
val position = getPositionForClip(currentlyPlaying.clip.clipID)
position.postValue(mediaPlayer.currentPosition)
}
}, 500)
}, timeBetweenUpdates.toLong())
}

private fun cancelPositionUpdates() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.odk.collect.android.audio;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
Expand All @@ -40,9 +41,9 @@ public class AudioControllerView extends FrameLayout {
private final SeekBar seekBar;
private final SwipeListener swipeListener;

private Boolean playing = false;
private Integer position = 0;
private Integer duration = 0;
private boolean playing;
private int position;
private int duration;

private Listener listener;

Expand Down Expand Up @@ -137,7 +138,12 @@ private void renderPosition(Integer position) {
this.position = position;

currentDurationLabel.setText(getTime(position));
seekBar.setProgress(position);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
seekBar.setProgress(position, true);
} else {
seekBar.setProgress(position);
}
}

public interface SwipableParent {
Expand Down

0 comments on commit 0442050

Please sign in to comment.