Skip to content

Commit 4efd94d

Browse files
authored
Merge pull request libre-tube#7277 from Bnyro/master
feat: make player default settings behavior more intuitive
2 parents 8355232 + 214e53d commit 4efd94d

6 files changed

Lines changed: 58 additions & 121 deletions

File tree

app/src/main/java/com/github/libretube/constants/PreferenceKeys.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ object PreferenceKeys {
4848
const val AUTOPLAY = "autoplay"
4949
const val RELATED_STREAMS = "related_streams_toggle"
5050
const val REMEMBER_PLAYBACK_SPEED = "remember_playback_speed"
51-
const val CUSTOM_PLAYBACK_SPEED = "custom_playback_speed"
5251
const val PLAYBACK_SPEED = "playback_speed"
53-
const val BACKGROUND_PLAYBACK_SPEED = "background_playback_speed"
5452
const val FULLSCREEN_ORIENTATION = "fullscreen_orientation"
5553
const val PAUSE_ON_SCREEN_OFF = "pause_screen_off"
5654
const val WATCH_POSITIONS = "watch_positions"
@@ -68,7 +66,7 @@ object PreferenceKeys {
6866
const val PLAYER_AUDIO_QUALITY_MOBILE = "player_audio_quality_mobile"
6967
const val DEFAULT_SUBTITLE = "default_subtitle"
7068
const val SKIP_BUTTONS = "skip_buttons"
71-
const val PLAYER_RESIZE_MODE = "player_resize_mode"
69+
const val PLAYER_RESIZE_MODE = "current_player_resize_mode"
7270
const val USE_HLS_OVER_DASH = "use_hls"
7371
const val QUEUE_AUTO_INSERT_RELATED = "queue_insert_related_videos"
7472
const val AUTOPLAY_PLAYLISTS = "autoplay_playlists"

app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -267,26 +267,12 @@ object PlayerHelper {
267267
.roundToInt()
268268
.toLong() * 1000
269269

270-
private val playbackSpeed: Float
270+
private val defaultPlaybackSpeed: Float
271271
get() = PreferenceHelper.getString(
272272
PreferenceKeys.PLAYBACK_SPEED,
273273
"1"
274274
).replace("F", "").toFloat()
275275

276-
private val backgroundSpeed: Float
277-
get() = when (PreferenceHelper.getBoolean(PreferenceKeys.CUSTOM_PLAYBACK_SPEED, false)) {
278-
true -> PreferenceHelper.getString(PreferenceKeys.BACKGROUND_PLAYBACK_SPEED, "1")
279-
.toFloat()
280-
281-
else -> playbackSpeed
282-
}
283-
284-
val resizeModePref: String
285-
get() = PreferenceHelper.getString(
286-
PreferenceKeys.PLAYER_RESIZE_MODE,
287-
"fit"
288-
)
289-
290276
val autoInsertRelatedVideos: Boolean
291277
get() = PreferenceHelper.getBoolean(
292278
PreferenceKeys.QUEUE_AUTO_INSERT_RELATED,
@@ -526,11 +512,7 @@ object PlayerHelper {
526512
* Create a basic player, that is used for all types of playback situations inside the app
527513
*/
528514
@OptIn(androidx.media3.common.util.UnstableApi::class)
529-
fun createPlayer(
530-
context: Context,
531-
trackSelector: DefaultTrackSelector,
532-
isBackgroundMode: Boolean
533-
): ExoPlayer {
515+
fun createPlayer(context: Context, trackSelector: DefaultTrackSelector, ): ExoPlayer {
534516
val dataSourceFactory = DefaultDataSource.Factory(context)
535517
val audioAttributes = AudioAttributes.Builder()
536518
.setUsage(C.USAGE_MEDIA)
@@ -546,7 +528,7 @@ object PlayerHelper {
546528
.setAudioAttributes(audioAttributes, handleAudioFocus)
547529
.build()
548530
.apply {
549-
loadPlaybackParams(isBackgroundMode)
531+
loadPlaybackParams()
550532
}
551533
}
552534

@@ -571,10 +553,10 @@ object PlayerHelper {
571553
* Load playback parameters such as speed and skip silence
572554
*/
573555
@OptIn(androidx.media3.common.util.UnstableApi::class)
574-
fun ExoPlayer.loadPlaybackParams(isBackgroundMode: Boolean = false): ExoPlayer {
556+
fun ExoPlayer.loadPlaybackParams(): ExoPlayer {
575557
skipSilenceEnabled = skipSilence
576-
val speed = if (isBackgroundMode) backgroundSpeed else playbackSpeed
577-
playbackParameters = PlaybackParameters(speed, 1.0f)
558+
559+
playbackParameters = PlaybackParameters(defaultPlaybackSpeed, 1.0f)
578560
return this
579561
}
580562

app/src/main/java/com/github/libretube/services/AbstractPlayerService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
316316
val trackSelector = DefaultTrackSelector(this)
317317
this.trackSelector = trackSelector
318318

319-
val player = PlayerHelper.createPlayer(this, trackSelector, true)
319+
val player = PlayerHelper.createPlayer(this, trackSelector)
320320
// prevent android from putting LibreTube to sleep when locked
321321
player.setWakeMode(if (isOfflinePlayer) C.WAKE_MODE_LOCAL else C.WAKE_MODE_NETWORK)
322322
player.addListener(playerListener)

app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,22 @@ abstract class CustomExoPlayerView(
106106
updateCurrentPosition()
107107
}
108108

109-
/**
110-
* Preferences
111-
*/
112-
private var resizeModePref = PlayerHelper.resizeModePref
109+
private var resizeModePref: Int
110+
set(value) {
111+
PreferenceHelper.putInt(
112+
PreferenceKeys.PLAYER_RESIZE_MODE,
113+
value
114+
)
115+
}
116+
get() = PreferenceHelper.getInt(
117+
PreferenceKeys.PLAYER_RESIZE_MODE,
118+
AspectRatioFrameLayout.RESIZE_MODE_FIT
119+
)
120+
private val resizeModes = listOf(
121+
AspectRatioFrameLayout.RESIZE_MODE_FIT to R.string.resize_mode_fit,
122+
AspectRatioFrameLayout.RESIZE_MODE_ZOOM to R.string.resize_mode_zoom,
123+
AspectRatioFrameLayout.RESIZE_MODE_FILL to R.string.resize_mode_fill
124+
)
113125

114126
val activity get() = context as BaseActivity
115127

@@ -167,11 +179,7 @@ abstract class CustomExoPlayerView(
167179
if (isFullscreen()) toggleSystemBars(!isPlayerLocked)
168180
}
169181

170-
resizeMode = when (resizeModePref) {
171-
"fill" -> AspectRatioFrameLayout.RESIZE_MODE_FILL
172-
"zoom" -> AspectRatioFrameLayout.RESIZE_MODE_ZOOM
173-
else -> AspectRatioFrameLayout.RESIZE_MODE_FIT
174-
}
182+
resizeMode = resizeModePref
175183

176184
binding.playPauseBTN.setOnClickListener {
177185
player?.togglePlayPauseState()
@@ -418,16 +426,8 @@ abstract class CustomExoPlayerView(
418426
context.getString(R.string.player_resize_mode),
419427
R.drawable.ic_aspect_ratio,
420428
{
421-
when (resizeMode) {
422-
AspectRatioFrameLayout.RESIZE_MODE_FIT -> context.getString(
423-
R.string.resize_mode_fit
424-
)
425-
426-
AspectRatioFrameLayout.RESIZE_MODE_FILL -> context.getString(
427-
R.string.resize_mode_fill
428-
)
429-
430-
else -> context.getString(R.string.resize_mode_zoom)
429+
resizeModes.find { it.first == resizeMode }?.second?.let {
430+
context.getString(it)
431431
}
432432
}
433433
) {
@@ -625,25 +625,24 @@ abstract class CustomExoPlayerView(
625625

626626
override fun onResizeModeClicked() {
627627
// switching between original aspect ratio (black bars) and zoomed to fill device screen
628-
val aspectRatioModeNames = context.resources?.getStringArray(R.array.resizeMode)
629-
?.toList().orEmpty()
630-
631-
val aspectRatioModes = listOf(
632-
AspectRatioFrameLayout.RESIZE_MODE_FIT,
633-
AspectRatioFrameLayout.RESIZE_MODE_ZOOM,
634-
AspectRatioFrameLayout.RESIZE_MODE_FILL
635-
)
636-
637628
BaseBottomSheet()
638629
.setSimpleItems(
639-
aspectRatioModeNames,
640-
preselectedItem = aspectRatioModeNames[aspectRatioModes.indexOf(resizeMode)]
630+
resizeModes.map { context.getString(it.second) },
631+
preselectedItem = resizeModes.first { it.first == resizeMode }.second.let {
632+
context.getString(it)
633+
}
641634
) { index ->
642-
resizeMode = aspectRatioModes[index]
635+
resizeMode = resizeModes[index].first
643636
}
644637
.show(supportFragmentManager)
645638
}
646639

640+
override fun setResizeMode(resizeMode: Int) {
641+
super.setResizeMode(resizeMode)
642+
// automatically remember the resize mode for the next session
643+
resizeModePref = resizeMode
644+
}
645+
647646
override fun onRepeatModeClicked() {
648647
// repeat mode options dialog
649648
BaseBottomSheet()
@@ -805,6 +804,7 @@ abstract class CustomExoPlayerView(
805804
override fun onMinimize() {
806805
if (!PlayerHelper.pinchGestureEnabled) return
807806
resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT
807+
808808
subtitleView?.setBottomPaddingFraction(SubtitleView.DEFAULT_BOTTOM_PADDING_FRACTION)
809809
}
810810

app/src/main/res/values/array.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -354,18 +354,6 @@
354354
<item>unlimited</item>
355355
</string-array>
356356

357-
<string-array name="resizeMode">
358-
<item>@string/resize_mode_fit</item>
359-
<item>@string/resize_mode_zoom</item>
360-
<item>@string/resize_mode_fill</item>
361-
</string-array>
362-
363-
<string-array name="resizeModeValues">
364-
<item>fit</item>
365-
<item>zoom</item>
366-
<item>fill</item>
367-
</string-array>
368-
369357
<string-array name="cacheSize">
370358
<item>16MB</item>
371359
<item>32MB</item>

app/src/main/res/xml/player_settings.xml

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@
7878
app:title="@string/fullscreen_orientation"
7979
app:useSimpleSummaryProvider="true" />
8080

81+
<SwitchPreferenceCompat
82+
android:icon="@drawable/ic_speed"
83+
android:summary="@string/autoplay_countdown_summary"
84+
app:defaultValue="false"
85+
app:key="autoplay_countdown"
86+
app:title="@string/autoplay_countdown" />
87+
8188
</PreferenceCategory>
8289

8390
<PreferenceCategory app:title="@string/captions">
@@ -110,6 +117,12 @@
110117
app:valueFrom="8"
111118
app:valueTo="30" />
112119

120+
<ListPreference
121+
android:defaultValue=""
122+
android:icon="@drawable/ic_caption"
123+
app:key="default_subtitle"
124+
app:title="@string/default_subtitle_language" />
125+
113126
</PreferenceCategory>
114127

115128
<PreferenceCategory app:title="@string/queue">
@@ -128,37 +141,8 @@
128141

129142
</PreferenceCategory>
130143

131-
132144
<PreferenceCategory app:title="@string/defaults">
133145

134-
<ListPreference
135-
android:defaultValue=""
136-
android:icon="@drawable/ic_caption"
137-
app:key="default_subtitle"
138-
app:title="@string/default_subtitle_language" />
139-
140-
<ListPreference
141-
android:defaultValue="fit"
142-
android:entries="@array/resizeMode"
143-
android:entryValues="@array/resizeModeValues"
144-
android:icon="@drawable/ic_zoom"
145-
app:key="player_resize_mode"
146-
app:title="@string/player_resize_mode"
147-
app:useSimpleSummaryProvider="true" />
148-
149-
<SwitchPreferenceCompat
150-
android:icon="@drawable/ic_speed"
151-
android:summary="@string/autoplay_countdown_summary"
152-
app:defaultValue="false"
153-
app:key="autoplay_countdown"
154-
app:title="@string/autoplay_countdown" />
155-
156-
<SwitchPreferenceCompat
157-
android:defaultValue="false"
158-
android:icon="@drawable/ic_fullscreen"
159-
android:title="@string/auto_fullscreen_shorts"
160-
app:key="auto_fullscreen_shorts" />
161-
162146
<SwitchPreferenceCompat
163147
android:icon="@drawable/ic_headphones"
164148
android:summaryOff="@string/disabled"
@@ -180,27 +164,6 @@
180164

181165
</PreferenceCategory>
182166

183-
<PreferenceCategory app:title="@string/background_mode">
184-
185-
<SwitchPreferenceCompat
186-
android:icon="@drawable/ic_headphones"
187-
android:summary="@string/custom_playback_speed_summary"
188-
app:defaultValue="false"
189-
app:key="custom_playback_speed"
190-
app:title="@string/custom_playback_speed" />
191-
192-
<com.github.libretube.ui.views.SliderPreference
193-
android:dependency="custom_playback_speed"
194-
android:icon="@drawable/ic_speed"
195-
app:defValue="1.0"
196-
app:key="background_playback_speed"
197-
app:stepSize="0.1"
198-
app:title="@string/playback_speed"
199-
app:valueFrom="0.2"
200-
app:valueTo="4.0" />
201-
202-
</PreferenceCategory>
203-
204167
<PreferenceCategory app:title="@string/misc">
205168

206169
<com.github.libretube.ui.preferences.EditNumberPreference
@@ -224,6 +187,12 @@
224187
app:key="alternative_pip_controls"
225188
app:title="@string/alternative_pip_controls" />
226189

190+
<SwitchPreferenceCompat
191+
android:defaultValue="false"
192+
android:icon="@drawable/ic_fullscreen"
193+
android:title="@string/auto_fullscreen_shorts"
194+
app:key="auto_fullscreen_shorts" />
195+
227196
<SwitchPreferenceCompat
228197
android:icon="@drawable/ic_pause_filled"
229198
android:summary="@string/pauseOnScreenOff_summary"

0 commit comments

Comments
 (0)