From 3855e488cb78959dbf6327ff187fda8867e42b43 Mon Sep 17 00:00:00 2001 From: Xiang Rong Lin <41164160+XiangRongLin@users.noreply.github.com> Date: Fri, 3 Apr 2020 18:47:53 +0200 Subject: [PATCH] Save and restore playback parameters into/from preferences Playback parameters are speed, pitch and skip silence. Remove parameters being passed on as intent to the player, since the parameters can be restored from the preferences instead. # Conflicts: # app/src/main/java/org/schabi/newpipe/player/BasePlayer.java --- .../org/schabi/newpipe/player/BasePlayer.java | 40 ++++++++++++++----- .../schabi/newpipe/util/NavigationHelper.java | 3 -- app/src/main/res/values/settings_keys.xml | 3 ++ 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java index 601fd96bf32..ecae1329001 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java @@ -118,12 +118,6 @@ public abstract class BasePlayer implements @NonNull public static final String REPEAT_MODE = "repeat_mode"; @NonNull - public static final String PLAYBACK_PITCH = "playback_pitch"; - @NonNull - public static final String PLAYBACK_SPEED = "playback_speed"; - @NonNull - public static final String PLAYBACK_SKIP_SILENCE = "playback_skip_silence"; - @NonNull public static final String PLAYBACK_QUALITY = "playback_quality"; @NonNull public static final String PLAY_QUEUE_KEY = "play_queue_key"; @@ -287,11 +281,12 @@ public void handleIntent(final Intent intent) { return; } + final PlaybackParameters savedParameters = retrievePlaybackParametersFromPreferences(); + final float playbackSpeed = savedParameters.speed; + final float playbackPitch = savedParameters.pitch; + final boolean playbackSkipSilence = savedParameters.skipSilence; + final int repeatMode = intent.getIntExtra(REPEAT_MODE, getRepeatMode()); - final float playbackSpeed = intent.getFloatExtra(PLAYBACK_SPEED, getPlaybackSpeed()); - final float playbackPitch = intent.getFloatExtra(PLAYBACK_PITCH, getPlaybackPitch()); - final boolean playbackSkipSilence = intent.getBooleanExtra(PLAYBACK_SKIP_SILENCE, - getPlaybackSkipSilence()); final boolean isMuted = intent .getBooleanExtra(IS_MUTED, simpleExoPlayer != null && isMuted()); @@ -330,6 +325,20 @@ public void handleIntent(final Intent intent) { /*playOnInit=*/!intent.getBooleanExtra(START_PAUSED, false), isMuted); } + private PlaybackParameters retrievePlaybackParametersFromPreferences() { + final SharedPreferences preferences = + PreferenceManager.getDefaultSharedPreferences(context); + + final float speed = preferences + .getFloat(context.getString(R.string.playback_speed_key), getPlaybackSpeed()); + final float pitch = preferences.getFloat(context.getString(R.string.playback_pitch_key), + getPlaybackPitch()); + final boolean skipSilence = preferences + .getBoolean(context.getString(R.string.playback_skip_silence_key), + getPlaybackSkipSilence()); + return new PlaybackParameters(speed, pitch, skipSilence); + } + protected void initPlayback(@NonNull final PlayQueue queue, @Player.RepeatMode final int repeatMode, final float playbackSpeed, @@ -1470,9 +1479,20 @@ public PlaybackParameters getPlaybackParameters() { public void setPlaybackParameters(final float speed, final float pitch, final boolean skipSilence) { + savePlaybackParametersToPreferences(speed, pitch, skipSilence); simpleExoPlayer.setPlaybackParameters(new PlaybackParameters(speed, pitch, skipSilence)); } + private void savePlaybackParametersToPreferences(final float speed, final float pitch, + final boolean skipSilence) { + PreferenceManager.getDefaultSharedPreferences(context) + .edit() + .putFloat(context.getString(R.string.playback_speed_key), speed) + .putFloat(context.getString(R.string.playback_pitch_key), pitch) + .putBoolean(context.getString(R.string.playback_skip_silence_key), skipSilence) + .apply(); + } + public PlayQueue getPlayQueue() { return playQueue; } diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index 32c06257123..ccaa79f9888 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -120,9 +120,6 @@ public static Intent getPlayerIntent(@NonNull final Context context, final boolean isMuted) { return getPlayerIntent(context, targetClazz, playQueue, playbackQuality, resumePlayback) .putExtra(BasePlayer.REPEAT_MODE, repeatMode) - .putExtra(BasePlayer.PLAYBACK_SPEED, playbackSpeed) - .putExtra(BasePlayer.PLAYBACK_PITCH, playbackPitch) - .putExtra(BasePlayer.PLAYBACK_SKIP_SILENCE, playbackSkipSilence) .putExtra(BasePlayer.START_PAUSED, startPaused) .putExtra(BasePlayer.IS_MUTED, isMuted); } diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 980af094302..b5eeae6aa36 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -177,6 +177,9 @@ enable_playback_resume enable_playback_state_lists playback_unhook_key + playback_speed_key + playback_pitch_key + playback_skip_silence_key app_language_key enable_lock_screen_video_thumbnail