1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-16 13:50:49 +01:00

Merge pull request #3288 from XiangRongLin/save-playback

Save and restore playback parameters into/from preferences
This commit is contained in:
Tobias Groza 2020-04-08 21:38:53 +02:00 committed by GitHub
commit 65cd9751d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 13 deletions

View File

@ -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 abstract class BasePlayer implements
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 abstract class BasePlayer implements
/*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 abstract class BasePlayer implements
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;
}

View File

@ -120,9 +120,6 @@ public final class NavigationHelper {
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);
}

View File

@ -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>