Improve track selector parameters logic

This commit is contained in:
Bnyro 2022-12-22 11:24:50 +01:00
parent a2296802f0
commit 73a7e62e57
2 changed files with 23 additions and 16 deletions

View File

@ -1,6 +1,10 @@
package com.github.libretube.extensions package com.github.libretube.extensions
import com.google.android.exoplayer2.trackselection.TrackSelector import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
fun TrackSelector.updateParameters() { fun DefaultTrackSelector.updateParameters(
actions: DefaultTrackSelector.Parameters.Builder.() -> Unit
) = apply {
val params = buildUponParameters().apply(actions)
setParameters(params)
} }

View File

@ -59,6 +59,7 @@ import com.github.libretube.extensions.hideKeyboard
import com.github.libretube.extensions.query import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toStreamItem import com.github.libretube.extensions.toStreamItem
import com.github.libretube.extensions.updateParameters
import com.github.libretube.obj.ShareData import com.github.libretube.obj.ShareData
import com.github.libretube.obj.VideoResolution import com.github.libretube.obj.VideoResolution
import com.github.libretube.services.BackgroundMode import com.github.libretube.services.BackgroundMode
@ -1195,9 +1196,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
} }
private fun setPlayerResolution(resolution: Int) { private fun setPlayerResolution(resolution: Int) {
val params = trackSelector.buildUponParameters() trackSelector.updateParameters {
params.setMaxVideoSize(Int.MAX_VALUE, resolution).setMinVideoSize(Int.MIN_VALUE, resolution) setMaxVideoSize(Int.MAX_VALUE, resolution)
trackSelector.setParameters(params) setMinVideoSize(Int.MIN_VALUE, resolution)
}
} }
private fun setStreamSource() { private fun setStreamSource() {
@ -1257,10 +1259,11 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// control for the track sources like subtitles and audio source // control for the track sources like subtitles and audio source
trackSelector = DefaultTrackSelector(requireContext()) trackSelector = DefaultTrackSelector(requireContext())
val params = trackSelector.buildUponParameters().setPreferredAudioLanguage( val params = trackSelector.updateParameters {
Locale.getDefault().language.lowercase().substring(0, 2) setPreferredAudioLanguage(
) Locale.getDefault().language.lowercase().substring(0, 2)
trackSelector.setParameters(params) )
}
exoPlayer = ExoPlayer.Builder(requireContext()) exoPlayer = ExoPlayer.Builder(requireContext())
.setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory)) .setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory))
@ -1355,9 +1358,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
.setSimpleItems(audioLanguages) { index -> .setSimpleItems(audioLanguages) { index ->
val audioStreams = audioGroups.values.elementAt(index) val audioStreams = audioGroups.values.elementAt(index)
val lang = audioStreams.firstOrNull()?.audioTrackId?.substring(0, 2) val lang = audioStreams.firstOrNull()?.audioTrackId?.substring(0, 2)
val newParams = trackSelector.buildUponParameters() trackSelector.updateParameters {
.setPreferredAudioLanguage(lang) setPreferredAudioLanguage(lang)
trackSelector.setParameters(newParams) }
} }
.show(childFragmentManager) .show(childFragmentManager)
} }
@ -1407,10 +1410,10 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
} }
private fun updateCaptionsLanguage(language: String?) { private fun updateCaptionsLanguage(language: String?) {
val params = trackSelector.buildUponParameters() trackSelector.updateParameters {
.setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION) setPreferredTextRoleFlags(C.ROLE_FLAG_CAPTION)
.setPreferredTextLanguage(language) setPreferredTextLanguage(language)
trackSelector.setParameters(params) }
} }
fun onUserLeaveHint() { fun onUserLeaveHint() {