From 9aa0f6879df846a19e7b023c18481fa5af15bd64 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 2 Oct 2024 13:11:23 +0200 Subject: [PATCH] refactor: also respect codec preference in audio mode --- .../github/libretube/helpers/PlayerHelper.kt | 26 +++++++++++++++++++ .../libretube/services/OnlinePlayerService.kt | 8 +++--- .../libretube/ui/fragments/PlayerFragment.kt | 23 +--------------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt index 344643ed8..f106c81f2 100644 --- a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt @@ -869,4 +869,30 @@ object PlayerHelper { else -> false } } + + @OptIn(UnstableApi::class) + fun setPreferredCodecs(trackSelector: DefaultTrackSelector) { + trackSelector.updateParameters { + val enabledVideoCodecs = PlayerHelper.enabledVideoCodecs + if (enabledVideoCodecs != "all") { + // map the codecs to their corresponding mimetypes + val mimeType = when (enabledVideoCodecs) { + "vp9" -> arrayOf("video/webm", "video/x-vnd.on2.vp9") + "avc" -> arrayOf("video/mp4", "video/avc") + else -> throw IllegalArgumentException() + } + this.setPreferredVideoMimeTypes(*mimeType) + } + val enabledAudioCodecs = PlayerHelper.enabledAudioCodecs + if (enabledAudioCodecs != "all") { + // map the codecs to their corresponding mimetypes + val mimeType = when (enabledAudioCodecs) { + "opus" -> arrayOf("audio/opus") + "mp4" -> arrayOf("audio/mp4a-latm") + else -> throw IllegalArgumentException() + } + this.setPreferredAudioMimeTypes(*mimeType) + } + } + } } diff --git a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt index 2052c1dfd..a88e8011f 100644 --- a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt @@ -338,11 +338,11 @@ class OnlinePlayerService : LifecycleService() { // prevent android from putting LibreTube to sleep when locked player!!.setWakeMode(WAKE_MODE_NETWORK) - /** - * Listens for changed playbackStates (e.g. pause, end) - * Plays the next video when the current one ended - */ + // Listens for changed playbackStates (e.g. pause, end) + // Plays the next video when the current one ended player?.addListener(playerListener) + + PlayerHelper.setPreferredCodecs(trackSelector!!) } /** diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index bff3e6f30..e06803dc7 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -1348,28 +1348,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { viewModel.player.addListener(playerListener) // control for the track sources like subtitles and audio source - viewModel.trackSelector.updateParameters { - val enabledVideoCodecs = PlayerHelper.enabledVideoCodecs - if (enabledVideoCodecs != "all") { - // map the codecs to their corresponding mimetypes - val mimeType = when (enabledVideoCodecs) { - "vp9" -> arrayOf("video/webm", "video/x-vnd.on2.vp9") - "avc" -> arrayOf("video/mp4", "video/avc") - else -> throw IllegalArgumentException() - } - this.setPreferredVideoMimeTypes(*mimeType) - } - val enabledAudioCodecs = PlayerHelper.enabledAudioCodecs - if (enabledAudioCodecs != "all") { - // map the codecs to their corresponding mimetypes - val mimeType = when (enabledAudioCodecs) { - "opus" -> arrayOf("audio/opus") - "mp4" -> arrayOf("audio/mp4a-latm") - else -> throw IllegalArgumentException() - } - this.setPreferredAudioMimeTypes(*mimeType) - } - } + PlayerHelper.setPreferredCodecs(viewModel.trackSelector) } /**