Fix audio/video desync caused by floating point cumulative errors

This commit is contained in:
Stypox 2020-06-30 22:41:09 +02:00
parent d9100913d5
commit 38c79bbc11
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
1 changed files with 19 additions and 9 deletions

View File

@ -331,13 +331,12 @@ public abstract class BasePlayer implements
final SharedPreferences preferences = final SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(context); PreferenceManager.getDefaultSharedPreferences(context);
final float speed = preferences final float speed = preferences.getFloat(
.getFloat(context.getString(R.string.playback_speed_key), getPlaybackSpeed()); context.getString(R.string.playback_speed_key), getPlaybackSpeed());
final float pitch = preferences.getFloat(context.getString(R.string.playback_pitch_key), final float pitch = preferences.getFloat(
getPlaybackPitch()); context.getString(R.string.playback_pitch_key), getPlaybackPitch());
final boolean skipSilence = preferences final boolean skipSilence = preferences.getBoolean(
.getBoolean(context.getString(R.string.playback_skip_silence_key), context.getString(R.string.playback_skip_silence_key), getPlaybackSkipSilence());
getPlaybackSkipSilence());
return new PlaybackParameters(speed, pitch, skipSilence); return new PlaybackParameters(speed, pitch, skipSilence);
} }
@ -1471,10 +1470,21 @@ public abstract class BasePlayer implements
return parameters == null ? PlaybackParameters.DEFAULT : parameters; return parameters == null ? PlaybackParameters.DEFAULT : parameters;
} }
/**
* Sets the playback parameters of the player, and also saves them to shared preferences.
* Speed and pitch are rounded up to 2 decimal places before being used or saved.
* @param speed the playback speed, will be rounded to up to 2 decimal places
* @param pitch the playback pitch, will be rounded to up to 2 decimal places
* @param skipSilence skip silence during playback
*/
public void setPlaybackParameters(final float speed, final float pitch, public void setPlaybackParameters(final float speed, final float pitch,
final boolean skipSilence) { final boolean skipSilence) {
savePlaybackParametersToPreferences(speed, pitch, skipSilence); final float roundedSpeed = Math.round(speed * 100.0f) / 100.0f;
simpleExoPlayer.setPlaybackParameters(new PlaybackParameters(speed, pitch, skipSilence)); final float roundedPitch = Math.round(pitch * 100.0f) / 100.0f;
savePlaybackParametersToPreferences(roundedSpeed, roundedPitch, skipSilence);
simpleExoPlayer.setPlaybackParameters(
new PlaybackParameters(roundedSpeed, roundedPitch, skipSilence));
} }
private void savePlaybackParametersToPreferences(final float speed, final float pitch, private void savePlaybackParametersToPreferences(final float speed, final float pitch,