feat: option to force lowest audio quality

This commit is contained in:
Bnyro 2023-07-19 10:09:05 +02:00
parent 08584b9bfc
commit eb386fdcdb
5 changed files with 32 additions and 13 deletions

View File

@ -27,6 +27,7 @@ import androidx.media3.common.Tracks
import androidx.media3.exoplayer.DefaultLoadControl
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.LoadControl
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
import androidx.media3.ui.CaptionStyleCompat
import com.github.libretube.R
import com.github.libretube.api.obj.ChapterSegment
@ -37,6 +38,7 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.enums.PlayerEvent
import com.github.libretube.enums.SbSkipOptions
import com.github.libretube.extensions.updateParameters
import com.github.libretube.obj.PreviewFrame
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.runBlocking
@ -331,6 +333,23 @@ object PlayerHelper {
return PreferenceHelper.getString(prefKey, "")
}
/**
* Apply the preferred audio quality: auto or worst
*/
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
fun applyPreferredAudioQuality(context: Context, trackSelector: DefaultTrackSelector) {
val prefKey = if (NetworkHelper.isNetworkMetered(context)) {
PreferenceKeys.PLAYER_AUDIO_QUALITY_MOBILE
} else {
PreferenceKeys.PLAYER_AUDIO_QUALITY
}
when (PreferenceHelper.getString(prefKey, "auto")) {
"worst" -> trackSelector.updateParameters {
setMaxAudioBitrate(1)
}
}
}
fun getIntentActon(context: Context): String {
return context.packageName + "." + ACTION_MEDIA_CONTROL
}
@ -659,7 +678,7 @@ object PlayerHelper {
*
* Duplicate audio languages with their role flags are removed.
*
* @param groups the list of [Group]s of the current tracks played by the player
* @param groups the list of [Tracks.Group]s of the current tracks played by the player
* @param keepOnlySelectedTracks whether to get only the selected audio languages with their
* role flags among the supported ones
* @return a list of distinct audio languages with their role flags from the supported formats
@ -697,9 +716,7 @@ object PlayerHelper {
* @param flag a flag to check its presence in the given bitfield
* @return whether the given flag is set in the given bitfield
*/
private fun isFlagSet(bitField: Int, flag: Int): Boolean {
return bitField and flag == flag;
}
private fun isFlagSet(bitField: Int, flag: Int) = bitField and flag == flag
/**
* Check whether the given ExoPlayer role flags contain at least one flag used for audio

View File

@ -17,6 +17,7 @@ import androidx.media3.common.MimeTypes
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
import com.github.libretube.R
import com.github.libretube.api.JsonHelper
import com.github.libretube.api.RetrofitInstance
@ -230,11 +231,15 @@ class OnlinePlayerService : LifecycleService() {
private fun initializePlayer() {
if (player != null) return
val trackSelector = DefaultTrackSelector(this)
PlayerHelper.applyPreferredAudioQuality(this, trackSelector)
player = ExoPlayer.Builder(this)
.setUsePlatformDiagnostics(false)
.setHandleAudioBecomingNoisy(true)
.setAudioAttributes(PlayerHelper.getAudioAttributes(), true)
.setLoadControl(PlayerHelper.getLoadControl())
.setTrackSelector(trackSelector)
.build()
.loadPlaybackParams(isBackgroundMode = true)

View File

@ -1360,6 +1360,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
}
PlayerHelper.applyPreferredAudioQuality(requireContext(), trackSelector)
exoPlayer = ExoPlayer.Builder(requireContext())
.setUsePlatformDiagnostics(false)
.setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory))

View File

@ -259,12 +259,12 @@
</string-array>
<string-array name="audioQuality">
<item>@string/best_quality</item>
<item>@string/auto</item>
<item>@string/worst_quality</item>
</string-array>
<string-array name="audioQualityValues">
<item>best</item>
<item>auto</item>
<item>worst</item>
</string-array>

View File

@ -13,16 +13,14 @@
app:title="@string/defres"
app:useSimpleSummaryProvider="true" />
<!--
<ListPreference
android:icon="@drawable/ic_headphones"
app:defaultValue="best"
app:defaultValue="auto"
app:entries="@array/audioQuality"
app:entryValues="@array/audioQualityValues"
app:key="player_audio_quality"
app:title="@string/playerAudioQuality"
app:useSimpleSummaryProvider="true" />
-->
</PreferenceCategory>
@ -37,20 +35,17 @@
app:title="@string/defres"
app:useSimpleSummaryProvider="true" />
<!--
<ListPreference
android:icon="@drawable/ic_headphones"
app:defaultValue="best"
app:defaultValue="auto"
app:entries="@array/audioQuality"
app:entryValues="@array/audioQualityValues"
app:key="player_audio_quality_mobile"
app:title="@string/playerAudioQuality"
app:useSimpleSummaryProvider="true" />
-->
</PreferenceCategory>
<PreferenceCategory app:title="@string/quality">
<ListPreference