Merge pull request #3837 from Stypox/audio-sync

Fix audio/video desync caused by floating point cumulative errors
This commit is contained in:
Tobias Groza 2020-07-02 21:29:18 +02:00 committed by GitHub
commit 9516d9da17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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,