diff --git a/app/src/main/java/com/github/libretube/enums/AudioQuality.kt b/app/src/main/java/com/github/libretube/enums/AudioQuality.kt new file mode 100644 index 000000000..69f8826fe --- /dev/null +++ b/app/src/main/java/com/github/libretube/enums/AudioQuality.kt @@ -0,0 +1,6 @@ +package com.github.libretube.enums + +enum class AudioQuality { + BEST, + WORST +} diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index bddcb5fde..1c8e569cc 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -293,22 +293,23 @@ class BackgroundMode : Service() { * Sets the [MediaItem] with the [streams] into the [player] */ private fun setMediaItem() { - streams?.let { - val uri = if (streams!!.hls != null) { - streams!!.hls - } else if (streams!!.audioStreams!!.isNotEmpty()) { - PlayerHelper.getAudioSource( - this, - streams!!.audioStreams!! - ) - } else { - return - } - val mediaItem = MediaItem.Builder() - .setUri(uri) - .build() - player?.setMediaItem(mediaItem) + streams ?: return + + val uri = if (streams!!.audioStreams.orEmpty().isNotEmpty()) { + PlayerHelper.getAudioSource( + this, + streams!!.audioStreams!! + ) + } else if (streams!!.hls != null) { + streams!!.hls + } else { + return } + + val mediaItem = MediaItem.Builder() + .setUri(uri) + .build() + player?.setMediaItem(mediaItem) } /** diff --git a/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt b/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt index 04fd05430..90f35d2f2 100644 --- a/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt +++ b/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt @@ -323,10 +323,14 @@ internal class CustomExoPlayerView( // hide the dimming background overlay if locked binding.exoControlsBackground.setBackgroundColor( - if (isLocked) ContextCompat.getColor( - context, - com.google.android.exoplayer2.R.color.exo_black_opacity_60 - ) else Color.TRANSPARENT + if (isLocked) { + ContextCompat.getColor( + context, + com.google.android.exoplayer2.R.color.exo_black_opacity_60 + ) + } else { + Color.TRANSPARENT + } ) // disable tap and swipe gesture if the player is locked diff --git a/app/src/main/java/com/github/libretube/util/PlayerHelper.kt b/app/src/main/java/com/github/libretube/util/PlayerHelper.kt index c4a65a880..cf54fd0c4 100644 --- a/app/src/main/java/com/github/libretube/util/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PlayerHelper.kt @@ -5,64 +5,57 @@ import android.content.pm.ActivityInfo import android.view.accessibility.CaptioningManager import com.github.libretube.api.obj.PipedStream import com.github.libretube.constants.PreferenceKeys +import com.github.libretube.enums.AudioQuality import com.google.android.exoplayer2.ui.CaptionStyleCompat import com.google.android.exoplayer2.video.VideoSize import kotlin.math.roundToInt object PlayerHelper { - // get the audio source following the users preferences + /** + * Get the audio source following the users preferences + */ fun getAudioSource( context: Context, audios: List ): String { val audioFormat = PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_FORMAT, "all") - val audioQuality = if ( + val audioPrefKey = if ( NetworkHelper.isNetworkMobile(context) ) { - PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_QUALITY_MOBILE, "best") + PreferenceKeys.PLAYER_AUDIO_QUALITY_MOBILE } else { - PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_QUALITY, "best") + PreferenceKeys.PLAYER_AUDIO_QUALITY } - val mutableAudios = audios.toMutableList() - if (audioFormat != "all") { - audios.forEach { - val audioMimeType = "audio/$audioFormat" - if (it.mimeType != audioMimeType) mutableAudios.remove(it) - } + val audioQuality = PreferenceHelper.getString(audioPrefKey, "best") + + val filteredAudios = audios.filter { + val audioMimeType = "audio/$audioFormat" + it.mimeType != audioMimeType || audioFormat == "all" } - return if (audioQuality == "worst") { - getLeastBitRate(mutableAudios) - } else { - getMostBitRate(mutableAudios) - } + return getBitRate( + filteredAudios, + if (audioQuality == "best") AudioQuality.BEST else AudioQuality.WORST + ) } - // get the best bit rate from audio streams - private fun getMostBitRate(audios: List): String { - var bitrate = 0 - var audioUrl = "" - audios.forEach { - if (it.bitrate != null && it.bitrate!! > bitrate) { - bitrate = it.bitrate!! - audioUrl = it.url.toString() - } + /** + * Get the best or worst bitrate from a list of audio streams + * @param audios list of the audio streams + * @param getLeast whether the least bitrate should be returned + * @return Url of the audio source + */ + private fun getBitRate(audios: List, quality: AudioQuality): String { + val filteredAudios = audios.filter { + it.bitrate != null + }.sortedBy { + it.bitrate } - return audioUrl - } - - // get the best bit rate from audio streams - private fun getLeastBitRate(audios: List): String { - var bitrate = 1000000000 - var audioUrl = "" - audios.forEach { - if (it.bitrate != null && it.bitrate!! < bitrate) { - bitrate = it.bitrate!! - audioUrl = it.url.toString() - } - } - return audioUrl + return when (quality) { + AudioQuality.BEST -> filteredAudios.last() + AudioQuality.WORST -> filteredAudios.first() + }.url!! } // get the system default caption style