diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 8c4e6f420..84ba5fb56 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -160,7 +160,7 @@ class PlayerFragment : Fragment() { private var watchPositionsEnabled = true private var useSystemCaptionStyle = true private var seekIncrement = 5L - private var videoFormatPreference = "WEBM" + private var videoFormatPreference = "webm" private var defRes = "" private var bufferingGoal = 50000 private var seekBarPreview = false @@ -287,7 +287,7 @@ class PlayerFragment : Fragment() { videoFormatPreference = PreferenceHelper.getString( PreferenceKeys.PLAYER_VIDEO_FORMAT, - "WEBM" + "webm" )!! defRes = PreferenceHelper.getString( @@ -1319,7 +1319,8 @@ class PlayerFragment : Fragment() { for (vid in response.videoStreams!!) { // append quality to list if it has the preferred format (e.g. MPEG) - if (vid.format.equals(videoFormatPreference) && vid.url != null) { // preferred format + val preferredMimeType = "video/$videoFormatPreference" + if (vid.url != null && vid.mimeType == preferredMimeType) { // preferred format videosNameArray += vid.quality.toString() videosUrlArray += vid.url!!.toUri() } else if (vid.quality.equals("LBRY") && vid.format.equals("MP4")) { // LBRY MP4 format @@ -1410,7 +1411,7 @@ class PlayerFragment : Fragment() { exoPlayer.setMediaItem(mediaItem) } else { val videoUri = videosUrlArray[which] - val audioUrl = PlayerHelper.getMostBitRate(response.audioStreams!!) + val audioUrl = PlayerHelper.getAudioSource(response.audioStreams!!) setMediaSource(videoUri, audioUrl) } exoPlayer.seekTo(lastPosition) @@ -1431,7 +1432,7 @@ class PlayerFragment : Fragment() { // search for quality preference in the available stream sources if (pipedStream.contains(defRes)) { val videoUri = videosUrlArray[index] - val audioUrl = PlayerHelper.getMostBitRate(streams.audioStreams!!) + val audioUrl = PlayerHelper.getAudioSource(streams.audioStreams!!) setMediaSource(videoUri, audioUrl) playerBinding.qualityText.text = videosNameArray[index] return @@ -1453,7 +1454,7 @@ class PlayerFragment : Fragment() { // if nothing found, use the first list entry if (videosUrlArray.isNotEmpty()) { val videoUri = videosUrlArray[0] - val audioUrl = PlayerHelper.getMostBitRate(streams.audioStreams!!) + val audioUrl = PlayerHelper.getAudioSource(streams.audioStreams!!) setMediaSource(videoUri, audioUrl) playerBinding.qualityText.text = videosNameArray[0] } diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt index 6b6332577..9065b83d2 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt @@ -54,6 +54,8 @@ object PreferenceKeys { const val DEFAULT_RESOLUTION = "default_res" const val BUFFERING_GOAL = "buffering_goal" const val SEEKBAR_PREVIEW = "seekbar_preview" + const val PLAYER_AUDIO_FORMAT = "player_audio_format" + const val PLAYER_AUDIO_QUALITY = "player_audio_quality" /** * Download 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 96282452d..ec869d974 100644 --- a/app/src/main/java/com/github/libretube/util/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PlayerHelper.kt @@ -3,13 +3,35 @@ package com.github.libretube.util import android.content.Context import android.view.accessibility.CaptioningManager import com.github.libretube.obj.PipedStream +import com.github.libretube.preferences.PreferenceHelper +import com.github.libretube.preferences.PreferenceKeys import com.google.android.exoplayer2.ui.CaptionStyleCompat object PlayerHelper { private val TAG = "PlayerHelper" + // get the audio source following the users preferences + fun getAudioSource(audios: List): String { + val audioFormat = PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_FORMAT, "all") + val audioQuality = PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_QUALITY, "best") + + val mutableAudios = audios.toMutableList() + if (audioFormat != "all") { + audios.forEach { + val audioMimeType = "audio/$audioFormat" + if (it.mimeType != audioMimeType) mutableAudios.remove(it) + } + } + + return if (audioQuality == "worst") { + getLeastBitRate(mutableAudios) + } else { + getMostBitRate(mutableAudios) + } + } + // get the best bit rate from audio streams - fun getMostBitRate(audios: List): String { + private fun getMostBitRate(audios: List): String { var bitrate = 0 var audioUrl = "" audios.forEach { @@ -21,6 +43,19 @@ object PlayerHelper { 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 + } + // get the system default caption style fun getCaptionStyle(context: Context): CaptionStyleCompat { val captioningManager = diff --git a/app/src/main/res/drawable/ic_music.xml b/app/src/main/res/drawable/ic_music.xml new file mode 100644 index 000000000..61c1fba86 --- /dev/null +++ b/app/src/main/res/drawable/ic_music.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml index a3346533f..e28789a05 100644 --- a/app/src/main/res/values/array.xml +++ b/app/src/main/res/values/array.xml @@ -690,11 +690,16 @@ 450 - + WEBM MPEG_4 + + webm + mpeg + + 5s 10s @@ -733,4 +738,26 @@ never + + @string/all + OPUS + M4A + + + + all + webm + mp4 + + + + @string/best_quality + @string/worst_quality + + + + best + worst + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c0b890ef7..d234e2b91 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -260,4 +260,8 @@ Playing on background … Caption settings Downloading Apk … + Audio format for player + Audio quality + Best quality + Worst quality \ No newline at end of file diff --git a/app/src/main/res/xml/player_settings.xml b/app/src/main/res/xml/player_settings.xml index a7d71a119..32c0d5822 100644 --- a/app/src/main/res/xml/player_settings.xml +++ b/app/src/main/res/xml/player_settings.xml @@ -13,15 +13,33 @@ app:title="@string/defres" app:useSimpleSummaryProvider="true" /> + + + +