feat: make player default settings behavior more intuitive

This commit is contained in:
Bnyro 2025-04-02 16:02:46 +02:00
parent 5cb0e7b76c
commit 214e53dc5d
No known key found for this signature in database
6 changed files with 58 additions and 121 deletions

View File

@ -48,9 +48,7 @@ object PreferenceKeys {
const val AUTOPLAY = "autoplay"
const val RELATED_STREAMS = "related_streams_toggle"
const val REMEMBER_PLAYBACK_SPEED = "remember_playback_speed"
const val CUSTOM_PLAYBACK_SPEED = "custom_playback_speed"
const val PLAYBACK_SPEED = "playback_speed"
const val BACKGROUND_PLAYBACK_SPEED = "background_playback_speed"
const val FULLSCREEN_ORIENTATION = "fullscreen_orientation"
const val PAUSE_ON_SCREEN_OFF = "pause_screen_off"
const val WATCH_POSITIONS = "watch_positions"
@ -68,7 +66,7 @@ object PreferenceKeys {
const val PLAYER_AUDIO_QUALITY_MOBILE = "player_audio_quality_mobile"
const val DEFAULT_SUBTITLE = "default_subtitle"
const val SKIP_BUTTONS = "skip_buttons"
const val PLAYER_RESIZE_MODE = "player_resize_mode"
const val PLAYER_RESIZE_MODE = "current_player_resize_mode"
const val USE_HLS_OVER_DASH = "use_hls"
const val QUEUE_AUTO_INSERT_RELATED = "queue_insert_related_videos"
const val AUTOPLAY_PLAYLISTS = "autoplay_playlists"

View File

@ -267,26 +267,12 @@ object PlayerHelper {
.roundToInt()
.toLong() * 1000
private val playbackSpeed: Float
private val defaultPlaybackSpeed: Float
get() = PreferenceHelper.getString(
PreferenceKeys.PLAYBACK_SPEED,
"1"
).replace("F", "").toFloat()
private val backgroundSpeed: Float
get() = when (PreferenceHelper.getBoolean(PreferenceKeys.CUSTOM_PLAYBACK_SPEED, false)) {
true -> PreferenceHelper.getString(PreferenceKeys.BACKGROUND_PLAYBACK_SPEED, "1")
.toFloat()
else -> playbackSpeed
}
val resizeModePref: String
get() = PreferenceHelper.getString(
PreferenceKeys.PLAYER_RESIZE_MODE,
"fit"
)
val autoInsertRelatedVideos: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.QUEUE_AUTO_INSERT_RELATED,
@ -526,11 +512,7 @@ object PlayerHelper {
* Create a basic player, that is used for all types of playback situations inside the app
*/
@OptIn(androidx.media3.common.util.UnstableApi::class)
fun createPlayer(
context: Context,
trackSelector: DefaultTrackSelector,
isBackgroundMode: Boolean
): ExoPlayer {
fun createPlayer(context: Context, trackSelector: DefaultTrackSelector, ): ExoPlayer {
val dataSourceFactory = DefaultDataSource.Factory(context)
val audioAttributes = AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
@ -546,7 +528,7 @@ object PlayerHelper {
.setAudioAttributes(audioAttributes, handleAudioFocus)
.build()
.apply {
loadPlaybackParams(isBackgroundMode)
loadPlaybackParams()
}
}
@ -571,10 +553,10 @@ object PlayerHelper {
* Load playback parameters such as speed and skip silence
*/
@OptIn(androidx.media3.common.util.UnstableApi::class)
fun ExoPlayer.loadPlaybackParams(isBackgroundMode: Boolean = false): ExoPlayer {
fun ExoPlayer.loadPlaybackParams(): ExoPlayer {
skipSilenceEnabled = skipSilence
val speed = if (isBackgroundMode) backgroundSpeed else playbackSpeed
playbackParameters = PlaybackParameters(speed, 1.0f)
playbackParameters = PlaybackParameters(defaultPlaybackSpeed, 1.0f)
return this
}

View File

@ -316,7 +316,7 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
val trackSelector = DefaultTrackSelector(this)
this.trackSelector = trackSelector
val player = PlayerHelper.createPlayer(this, trackSelector, true)
val player = PlayerHelper.createPlayer(this, trackSelector)
// prevent android from putting LibreTube to sleep when locked
player.setWakeMode(if (isOfflinePlayer) C.WAKE_MODE_LOCAL else C.WAKE_MODE_NETWORK)
player.addListener(playerListener)

View File

@ -106,10 +106,22 @@ abstract class CustomExoPlayerView(
updateCurrentPosition()
}
/**
* Preferences
*/
private var resizeModePref = PlayerHelper.resizeModePref
private var resizeModePref: Int
set(value) {
PreferenceHelper.putInt(
PreferenceKeys.PLAYER_RESIZE_MODE,
value
)
}
get() = PreferenceHelper.getInt(
PreferenceKeys.PLAYER_RESIZE_MODE,
AspectRatioFrameLayout.RESIZE_MODE_FIT
)
private val resizeModes = listOf(
AspectRatioFrameLayout.RESIZE_MODE_FIT to R.string.resize_mode_fit,
AspectRatioFrameLayout.RESIZE_MODE_ZOOM to R.string.resize_mode_zoom,
AspectRatioFrameLayout.RESIZE_MODE_FILL to R.string.resize_mode_fill
)
val activity get() = context as BaseActivity
@ -167,11 +179,7 @@ abstract class CustomExoPlayerView(
if (isFullscreen()) toggleSystemBars(!isPlayerLocked)
}
resizeMode = when (resizeModePref) {
"fill" -> AspectRatioFrameLayout.RESIZE_MODE_FILL
"zoom" -> AspectRatioFrameLayout.RESIZE_MODE_ZOOM
else -> AspectRatioFrameLayout.RESIZE_MODE_FIT
}
resizeMode = resizeModePref
binding.playPauseBTN.setOnClickListener {
player?.togglePlayPauseState()
@ -418,16 +426,8 @@ abstract class CustomExoPlayerView(
context.getString(R.string.player_resize_mode),
R.drawable.ic_aspect_ratio,
{
when (resizeMode) {
AspectRatioFrameLayout.RESIZE_MODE_FIT -> context.getString(
R.string.resize_mode_fit
)
AspectRatioFrameLayout.RESIZE_MODE_FILL -> context.getString(
R.string.resize_mode_fill
)
else -> context.getString(R.string.resize_mode_zoom)
resizeModes.find { it.first == resizeMode }?.second?.let {
context.getString(it)
}
}
) {
@ -625,25 +625,24 @@ abstract class CustomExoPlayerView(
override fun onResizeModeClicked() {
// switching between original aspect ratio (black bars) and zoomed to fill device screen
val aspectRatioModeNames = context.resources?.getStringArray(R.array.resizeMode)
?.toList().orEmpty()
val aspectRatioModes = listOf(
AspectRatioFrameLayout.RESIZE_MODE_FIT,
AspectRatioFrameLayout.RESIZE_MODE_ZOOM,
AspectRatioFrameLayout.RESIZE_MODE_FILL
)
BaseBottomSheet()
.setSimpleItems(
aspectRatioModeNames,
preselectedItem = aspectRatioModeNames[aspectRatioModes.indexOf(resizeMode)]
resizeModes.map { context.getString(it.second) },
preselectedItem = resizeModes.first { it.first == resizeMode }.second.let {
context.getString(it)
}
) { index ->
resizeMode = aspectRatioModes[index]
resizeMode = resizeModes[index].first
}
.show(supportFragmentManager)
}
override fun setResizeMode(resizeMode: Int) {
super.setResizeMode(resizeMode)
// automatically remember the resize mode for the next session
resizeModePref = resizeMode
}
override fun onRepeatModeClicked() {
// repeat mode options dialog
BaseBottomSheet()
@ -805,6 +804,7 @@ abstract class CustomExoPlayerView(
override fun onMinimize() {
if (!PlayerHelper.pinchGestureEnabled) return
resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT
subtitleView?.setBottomPaddingFraction(SubtitleView.DEFAULT_BOTTOM_PADDING_FRACTION)
}

View File

@ -354,18 +354,6 @@
<item>unlimited</item>
</string-array>
<string-array name="resizeMode">
<item>@string/resize_mode_fit</item>
<item>@string/resize_mode_zoom</item>
<item>@string/resize_mode_fill</item>
</string-array>
<string-array name="resizeModeValues">
<item>fit</item>
<item>zoom</item>
<item>fill</item>
</string-array>
<string-array name="cacheSize">
<item>16MB</item>
<item>32MB</item>

View File

@ -78,6 +78,13 @@
app:title="@string/fullscreen_orientation"
app:useSimpleSummaryProvider="true" />
<SwitchPreferenceCompat
android:icon="@drawable/ic_speed"
android:summary="@string/autoplay_countdown_summary"
app:defaultValue="false"
app:key="autoplay_countdown"
app:title="@string/autoplay_countdown" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/captions">
@ -110,6 +117,12 @@
app:valueFrom="8"
app:valueTo="30" />
<ListPreference
android:defaultValue=""
android:icon="@drawable/ic_caption"
app:key="default_subtitle"
app:title="@string/default_subtitle_language" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/queue">
@ -128,37 +141,8 @@
</PreferenceCategory>
<PreferenceCategory app:title="@string/defaults">
<ListPreference
android:defaultValue=""
android:icon="@drawable/ic_caption"
app:key="default_subtitle"
app:title="@string/default_subtitle_language" />
<ListPreference
android:defaultValue="fit"
android:entries="@array/resizeMode"
android:entryValues="@array/resizeModeValues"
android:icon="@drawable/ic_zoom"
app:key="player_resize_mode"
app:title="@string/player_resize_mode"
app:useSimpleSummaryProvider="true" />
<SwitchPreferenceCompat
android:icon="@drawable/ic_speed"
android:summary="@string/autoplay_countdown_summary"
app:defaultValue="false"
app:key="autoplay_countdown"
app:title="@string/autoplay_countdown" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:icon="@drawable/ic_fullscreen"
android:title="@string/auto_fullscreen_shorts"
app:key="auto_fullscreen_shorts" />
<SwitchPreferenceCompat
android:icon="@drawable/ic_headphones"
android:summaryOff="@string/disabled"
@ -180,27 +164,6 @@
</PreferenceCategory>
<PreferenceCategory app:title="@string/background_mode">
<SwitchPreferenceCompat
android:icon="@drawable/ic_headphones"
android:summary="@string/custom_playback_speed_summary"
app:defaultValue="false"
app:key="custom_playback_speed"
app:title="@string/custom_playback_speed" />
<com.github.libretube.ui.views.SliderPreference
android:dependency="custom_playback_speed"
android:icon="@drawable/ic_speed"
app:defValue="1.0"
app:key="background_playback_speed"
app:stepSize="0.1"
app:title="@string/playback_speed"
app:valueFrom="0.2"
app:valueTo="4.0" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/misc">
<com.github.libretube.ui.preferences.EditNumberPreference
@ -224,6 +187,12 @@
app:key="alternative_pip_controls"
app:title="@string/alternative_pip_controls" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:icon="@drawable/ic_fullscreen"
android:title="@string/auto_fullscreen_shorts"
app:key="auto_fullscreen_shorts" />
<SwitchPreferenceCompat
android:icon="@drawable/ic_pause_filled"
android:summary="@string/pauseOnScreenOff_summary"