mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 16:30:31 +05:30
Merge pull request #2633 from Bnyro/master
Reduce the amount of used data in background mode
This commit is contained in:
commit
855493b2a2
@ -0,0 +1,6 @@
|
|||||||
|
package com.github.libretube.enums
|
||||||
|
|
||||||
|
enum class AudioQuality {
|
||||||
|
BEST,
|
||||||
|
WORST
|
||||||
|
}
|
@ -293,23 +293,24 @@ class BackgroundMode : Service() {
|
|||||||
* Sets the [MediaItem] with the [streams] into the [player]
|
* Sets the [MediaItem] with the [streams] into the [player]
|
||||||
*/
|
*/
|
||||||
private fun setMediaItem() {
|
private fun setMediaItem() {
|
||||||
streams?.let {
|
streams ?: return
|
||||||
val uri = if (streams!!.hls != null) {
|
|
||||||
streams!!.hls
|
val uri = if (streams!!.audioStreams.orEmpty().isNotEmpty()) {
|
||||||
} else if (streams!!.audioStreams!!.isNotEmpty()) {
|
|
||||||
PlayerHelper.getAudioSource(
|
PlayerHelper.getAudioSource(
|
||||||
this,
|
this,
|
||||||
streams!!.audioStreams!!
|
streams!!.audioStreams!!
|
||||||
)
|
)
|
||||||
|
} else if (streams!!.hls != null) {
|
||||||
|
streams!!.hls
|
||||||
} else {
|
} else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val mediaItem = MediaItem.Builder()
|
val mediaItem = MediaItem.Builder()
|
||||||
.setUri(uri)
|
.setUri(uri)
|
||||||
.build()
|
.build()
|
||||||
player?.setMediaItem(mediaItem)
|
player?.setMediaItem(mediaItem)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fetch the segments for SponsorBlock
|
* fetch the segments for SponsorBlock
|
||||||
|
@ -323,10 +323,14 @@ internal class CustomExoPlayerView(
|
|||||||
|
|
||||||
// hide the dimming background overlay if locked
|
// hide the dimming background overlay if locked
|
||||||
binding.exoControlsBackground.setBackgroundColor(
|
binding.exoControlsBackground.setBackgroundColor(
|
||||||
if (isLocked) ContextCompat.getColor(
|
if (isLocked) {
|
||||||
|
ContextCompat.getColor(
|
||||||
context,
|
context,
|
||||||
com.google.android.exoplayer2.R.color.exo_black_opacity_60
|
com.google.android.exoplayer2.R.color.exo_black_opacity_60
|
||||||
) else Color.TRANSPARENT
|
)
|
||||||
|
} else {
|
||||||
|
Color.TRANSPARENT
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// disable tap and swipe gesture if the player is locked
|
// disable tap and swipe gesture if the player is locked
|
||||||
|
@ -5,64 +5,57 @@ import android.content.pm.ActivityInfo
|
|||||||
import android.view.accessibility.CaptioningManager
|
import android.view.accessibility.CaptioningManager
|
||||||
import com.github.libretube.api.obj.PipedStream
|
import com.github.libretube.api.obj.PipedStream
|
||||||
import com.github.libretube.constants.PreferenceKeys
|
import com.github.libretube.constants.PreferenceKeys
|
||||||
|
import com.github.libretube.enums.AudioQuality
|
||||||
import com.google.android.exoplayer2.ui.CaptionStyleCompat
|
import com.google.android.exoplayer2.ui.CaptionStyleCompat
|
||||||
import com.google.android.exoplayer2.video.VideoSize
|
import com.google.android.exoplayer2.video.VideoSize
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
object PlayerHelper {
|
object PlayerHelper {
|
||||||
// get the audio source following the users preferences
|
/**
|
||||||
|
* Get the audio source following the users preferences
|
||||||
|
*/
|
||||||
fun getAudioSource(
|
fun getAudioSource(
|
||||||
context: Context,
|
context: Context,
|
||||||
audios: List<PipedStream>
|
audios: List<PipedStream>
|
||||||
): String {
|
): String {
|
||||||
val audioFormat = PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_FORMAT, "all")
|
val audioFormat = PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_FORMAT, "all")
|
||||||
val audioQuality = if (
|
val audioPrefKey = if (
|
||||||
NetworkHelper.isNetworkMobile(context)
|
NetworkHelper.isNetworkMobile(context)
|
||||||
) {
|
) {
|
||||||
PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_QUALITY_MOBILE, "best")
|
PreferenceKeys.PLAYER_AUDIO_QUALITY_MOBILE
|
||||||
} else {
|
} else {
|
||||||
PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_QUALITY, "best")
|
PreferenceKeys.PLAYER_AUDIO_QUALITY
|
||||||
}
|
}
|
||||||
|
|
||||||
val mutableAudios = audios.toMutableList()
|
val audioQuality = PreferenceHelper.getString(audioPrefKey, "best")
|
||||||
if (audioFormat != "all") {
|
|
||||||
audios.forEach {
|
val filteredAudios = audios.filter {
|
||||||
val audioMimeType = "audio/$audioFormat"
|
val audioMimeType = "audio/$audioFormat"
|
||||||
if (it.mimeType != audioMimeType) mutableAudios.remove(it)
|
it.mimeType != audioMimeType || audioFormat == "all"
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return if (audioQuality == "worst") {
|
return getBitRate(
|
||||||
getLeastBitRate(mutableAudios)
|
filteredAudios,
|
||||||
} else {
|
if (audioQuality == "best") AudioQuality.BEST else AudioQuality.WORST
|
||||||
getMostBitRate(mutableAudios)
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the best bit rate from audio streams
|
/**
|
||||||
private fun getMostBitRate(audios: List<PipedStream>): String {
|
* Get the best or worst bitrate from a list of audio streams
|
||||||
var bitrate = 0
|
* @param audios list of the audio streams
|
||||||
var audioUrl = ""
|
* @param getLeast whether the least bitrate should be returned
|
||||||
audios.forEach {
|
* @return Url of the audio source
|
||||||
if (it.bitrate != null && it.bitrate!! > bitrate) {
|
*/
|
||||||
bitrate = it.bitrate!!
|
private fun getBitRate(audios: List<PipedStream>, quality: AudioQuality): String {
|
||||||
audioUrl = it.url.toString()
|
val filteredAudios = audios.filter {
|
||||||
|
it.bitrate != null
|
||||||
|
}.sortedBy {
|
||||||
|
it.bitrate
|
||||||
}
|
}
|
||||||
}
|
return when (quality) {
|
||||||
return audioUrl
|
AudioQuality.BEST -> filteredAudios.last()
|
||||||
}
|
AudioQuality.WORST -> filteredAudios.first()
|
||||||
|
}.url!!
|
||||||
// get the best bit rate from audio streams
|
|
||||||
private fun getLeastBitRate(audios: List<PipedStream>): 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
|
// get the system default caption style
|
||||||
|
Loading…
x
Reference in New Issue
Block a user