Skip to content

Commit 0eecef0

Browse files
fix(YouTube - Playback speed): Allow custom speeds with 0.01x precision (#5360)
1 parent 6a7402e commit 0eecef0

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public class CustomPlaybackSpeedPatch {
6060
*/
6161
public static final float PLAYBACK_SPEED_MAXIMUM = 8;
6262

63+
/**
64+
* How much +/- speed adjustment buttons change the current speed.
65+
*/
66+
private static final double SPEED_ADJUSTMENT_CHANGE = 0.05;
67+
6368
/**
6469
* Scale used to convert user speed to {@link android.widget.ProgressBar#setProgress(int)}.
6570
*/
@@ -390,9 +395,9 @@ public void onStopTrackingTouch(SeekBar seekBar) {}
390395
});
391396

392397
minusButton.setOnClickListener(v -> userSelectedSpeed.apply(
393-
VideoInformation.getPlaybackSpeed() - 0.05f));
398+
(float) (VideoInformation.getPlaybackSpeed() - SPEED_ADJUSTMENT_CHANGE)));
394399
plusButton.setOnClickListener(v -> userSelectedSpeed.apply(
395-
VideoInformation.getPlaybackSpeed() + 0.05f));
400+
(float) (VideoInformation.getPlaybackSpeed() + SPEED_ADJUSTMENT_CHANGE)));
396401

397402
// Create GridLayout for preset speed buttons.
398403
GridLayout gridLayout = new GridLayout(context);
@@ -611,15 +616,21 @@ private static int speedToProgressValue(float speed) {
611616
}
612617

613618
/**
614-
* Rounds the given playback speed to the nearest 0.05 increment and ensures it is within valid bounds.
619+
* Rounds the given playback speed to the nearest 0.05 increment,
620+
* unless the speed exactly matches a preset custom speed.
615621
*
616622
* @param speed The playback speed to round.
617623
* @return The rounded speed, constrained to the specified bounds.
618624
*/
619625
private static float roundSpeedToNearestIncrement(float speed) {
626+
// Allow speed as-is if it exactly matches a speed preset such as 1.03x.
627+
if (arrayContains(customPlaybackSpeeds, speed)) {
628+
return speed;
629+
}
630+
620631
// Round to nearest 0.05 speed. Must use double precision otherwise rounding error can occur.
621-
final double roundedSpeed = Math.round(speed / 0.05) * 0.05;
622-
return Utils.clamp((float) roundedSpeed, 0.05f, PLAYBACK_SPEED_MAXIMUM);
632+
final double roundedSpeed = Math.round(speed / SPEED_ADJUSTMENT_CHANGE) * SPEED_ADJUSTMENT_CHANGE;
633+
return Utils.clamp((float) roundedSpeed, (float) SPEED_ADJUSTMENT_CHANGE, PLAYBACK_SPEED_MAXIMUM);
623634
}
624635

625636
/**

0 commit comments

Comments
 (0)