Skip to content

Commit

Permalink
Save and restore playback parameters into/from preferences
Browse files Browse the repository at this point in the history
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
  • Loading branch information
XiangRongLin committed Apr 5, 2020
1 parent 5c8b9f6 commit 3855e48
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
40 changes: 30 additions & 10 deletions app/src/main/java/org/schabi/newpipe/player/BasePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/settings_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@
<string name="enable_playback_resume_key" translatable="false">enable_playback_resume</string>
<string name="enable_playback_state_lists_key" translatable="false">enable_playback_state_lists</string>
<string name="playback_unhook_key" translatable="false">playback_unhook_key</string>
<string name="playback_speed_key" translatable="false">playback_speed_key</string>
<string name="playback_pitch_key" translatable="false">playback_pitch_key</string>
<string name="playback_skip_silence_key" translatable="false">playback_skip_silence_key</string>

<string name="app_language_key" translatable="false">app_language_key</string>
<string name="enable_lock_screen_video_thumbnail_key" translatable="false">enable_lock_screen_video_thumbnail</string>
Expand Down

0 comments on commit 3855e48

Please sign in to comment.