Set min/max playback speeds from preferences

This commit is contained in:
Shinichiro Fujiwara 2019-02-27 22:29:28 +09:00
parent 335d287d5e
commit 6a56260157
1 changed files with 17 additions and 5 deletions

View File

@ -79,6 +79,9 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
private static final String PREFS = "MediaPlayerActivityPreferences"; private static final String PREFS = "MediaPlayerActivityPreferences";
private static final String PREF_SHOW_TIME_LEFT = "showTimeLeft"; private static final String PREF_SHOW_TIME_LEFT = "showTimeLeft";
private static final int REQUEST_CODE_STORAGE = 42; private static final int REQUEST_CODE_STORAGE = 42;
private static final float PLAYBACK_SPEED_STEP = 0.05f;
private static final float DEFAULT_MIN_PLAYBACK_SPEED = 0.5f;
private static final float DEFAULT_MAX_PLAYBACK_SPEED = 2.5f;
PlaybackController controller; PlaybackController controller;
@ -469,7 +472,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
final Button butDecSpeed = (Button) dialog.findViewById(R.id.butDecSpeed); final Button butDecSpeed = (Button) dialog.findViewById(R.id.butDecSpeed);
butDecSpeed.setOnClickListener(v -> { butDecSpeed.setOnClickListener(v -> {
if(controller != null && controller.canSetPlaybackSpeed()) { if(controller != null && controller.canSetPlaybackSpeed()) {
barPlaybackSpeed.setProgress(barPlaybackSpeed.getProgress() - 2); barPlaybackSpeed.setProgress(barPlaybackSpeed.getProgress() - 1);
} else { } else {
VariableSpeedDialog.showGetPluginDialog(this); VariableSpeedDialog.showGetPluginDialog(this);
} }
@ -477,7 +480,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
final Button butIncSpeed = (Button) dialog.findViewById(R.id.butIncSpeed); final Button butIncSpeed = (Button) dialog.findViewById(R.id.butIncSpeed);
butIncSpeed.setOnClickListener(v -> { butIncSpeed.setOnClickListener(v -> {
if(controller != null && controller.canSetPlaybackSpeed()) { if(controller != null && controller.canSetPlaybackSpeed()) {
barPlaybackSpeed.setProgress(barPlaybackSpeed.getProgress() + 2); barPlaybackSpeed.setProgress(barPlaybackSpeed.getProgress() + 1);
} else { } else {
VariableSpeedDialog.showGetPluginDialog(this); VariableSpeedDialog.showGetPluginDialog(this);
} }
@ -492,12 +495,20 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
UserPreferences.setPlaybackSpeed(String.valueOf(currentSpeed)); UserPreferences.setPlaybackSpeed(String.valueOf(currentSpeed));
} }
String[] availableSpeeds = UserPreferences.getPlaybackSpeedArray();
final float minPlaybackSpeed = availableSpeeds.length > 1 ?
Float.valueOf(availableSpeeds[0]) : DEFAULT_MIN_PLAYBACK_SPEED;
float maxPlaybackSpeed = availableSpeeds.length > 1 ?
Float.valueOf(availableSpeeds[availableSpeeds.length - 1]) : DEFAULT_MAX_PLAYBACK_SPEED;
int progressMax = (int) ((maxPlaybackSpeed - minPlaybackSpeed) / PLAYBACK_SPEED_STEP);
barPlaybackSpeed.setMax(progressMax);
txtvPlaybackSpeed.setText(String.format("%.2fx", currentSpeed)); txtvPlaybackSpeed.setText(String.format("%.2fx", currentSpeed));
barPlaybackSpeed.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { barPlaybackSpeed.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(controller != null && controller.canSetPlaybackSpeed()) { if(controller != null && controller.canSetPlaybackSpeed()) {
float playbackSpeed = (progress + 10) / 20.0f; float playbackSpeed = progress * PLAYBACK_SPEED_STEP + minPlaybackSpeed;
controller.setPlaybackSpeed(playbackSpeed); controller.setPlaybackSpeed(playbackSpeed);
String speedPref = String.format(Locale.US, "%.2f", playbackSpeed); String speedPref = String.format(Locale.US, "%.2f", playbackSpeed);
UserPreferences.setPlaybackSpeed(speedPref); UserPreferences.setPlaybackSpeed(speedPref);
@ -505,7 +516,8 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
txtvPlaybackSpeed.setText(speedStr); txtvPlaybackSpeed.setText(speedStr);
} else if(fromUser) { } else if(fromUser) {
float speed = Float.valueOf(UserPreferences.getPlaybackSpeed()); float speed = Float.valueOf(UserPreferences.getPlaybackSpeed());
barPlaybackSpeed.post(() -> barPlaybackSpeed.setProgress((int) (20 * speed) - 10)); barPlaybackSpeed.post(() -> barPlaybackSpeed.setProgress(
(int) ((speed - minPlaybackSpeed) / PLAYBACK_SPEED_STEP)));
} }
} }
@ -520,7 +532,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
public void onStopTrackingTouch(SeekBar seekBar) { public void onStopTrackingTouch(SeekBar seekBar) {
} }
}); });
barPlaybackSpeed.setProgress((int) (20 * currentSpeed) - 10); barPlaybackSpeed.setProgress((int) ((currentSpeed - minPlaybackSpeed) / PLAYBACK_SPEED_STEP));
final SeekBar barLeftVolume = (SeekBar) dialog.findViewById(R.id.volume_left); final SeekBar barLeftVolume = (SeekBar) dialog.findViewById(R.id.volume_left);
barLeftVolume.setProgress(UserPreferences.getLeftVolumePercentage()); barLeftVolume.setProgress(UserPreferences.getLeftVolumePercentage());