2022-07-17 01:01:15 +05:30
|
|
|
package com.github.libretube.util
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.view.accessibility.CaptioningManager
|
2022-09-08 21:59:00 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2022-09-20 23:23:34 +05:30
|
|
|
import com.github.libretube.api.obj.PipedStream
|
2022-07-17 01:01:15 +05:30
|
|
|
import com.google.android.exoplayer2.ui.CaptionStyleCompat
|
|
|
|
|
|
|
|
object PlayerHelper {
|
2022-07-24 16:29:15 +05:30
|
|
|
// get the audio source following the users preferences
|
2022-09-20 23:23:34 +05:30
|
|
|
fun getAudioSource(context: Context, audios: List<com.github.libretube.api.obj.PipedStream>): String {
|
2022-07-24 16:29:15 +05:30
|
|
|
val audioFormat = PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_FORMAT, "all")
|
2022-09-11 21:24:04 +05:30
|
|
|
val audioQuality = if (
|
|
|
|
NetworkHelper.isNetworkMobile(context)
|
|
|
|
) {
|
|
|
|
PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_QUALITY_MOBILE, "best")
|
|
|
|
} else {
|
|
|
|
PreferenceHelper.getString(PreferenceKeys.PLAYER_AUDIO_QUALITY, "best")
|
|
|
|
}
|
2022-07-24 16:29:15 +05:30
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 01:01:15 +05:30
|
|
|
// get the best bit rate from audio streams
|
2022-09-20 23:23:34 +05:30
|
|
|
private fun getMostBitRate(audios: List<com.github.libretube.api.obj.PipedStream>): String {
|
2022-07-17 01:01:15 +05:30
|
|
|
var bitrate = 0
|
2022-07-17 02:19:32 +05:30
|
|
|
var audioUrl = ""
|
|
|
|
audios.forEach {
|
2022-07-24 01:31:37 +05:30
|
|
|
if (it.bitrate != null && it.bitrate!! > bitrate) {
|
|
|
|
bitrate = it.bitrate!!
|
2022-07-17 02:19:32 +05:30
|
|
|
audioUrl = it.url.toString()
|
2022-07-17 01:01:15 +05:30
|
|
|
}
|
|
|
|
}
|
2022-07-17 02:19:32 +05:30
|
|
|
return audioUrl
|
2022-07-17 01:01:15 +05:30
|
|
|
}
|
|
|
|
|
2022-07-24 16:29:15 +05:30
|
|
|
// get the best bit rate from audio streams
|
2022-09-20 23:23:34 +05:30
|
|
|
private fun getLeastBitRate(audios: List<com.github.libretube.api.obj.PipedStream>): String {
|
2022-07-24 16:29:15 +05:30
|
|
|
var bitrate = 1000000000
|
|
|
|
var audioUrl = ""
|
|
|
|
audios.forEach {
|
|
|
|
if (it.bitrate != null && it.bitrate!! < bitrate) {
|
|
|
|
bitrate = it.bitrate!!
|
|
|
|
audioUrl = it.url.toString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return audioUrl
|
|
|
|
}
|
|
|
|
|
2022-07-17 01:01:15 +05:30
|
|
|
// get the system default caption style
|
|
|
|
fun getCaptionStyle(context: Context): CaptionStyleCompat {
|
2022-07-18 23:06:21 +05:30
|
|
|
val captioningManager =
|
|
|
|
context.getSystemService(Context.CAPTIONING_SERVICE) as CaptioningManager
|
2022-07-17 01:01:15 +05:30
|
|
|
return if (!captioningManager.isEnabled) {
|
|
|
|
// system captions are disabled, using android default captions style
|
|
|
|
CaptionStyleCompat.DEFAULT
|
|
|
|
} else {
|
|
|
|
// system captions are enabled
|
|
|
|
CaptionStyleCompat.createFromCaptionStyle(captioningManager.userStyle)
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 14:47:15 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* get the categories for sponsorBlock
|
|
|
|
*/
|
|
|
|
fun getSponsorBlockCategories(): ArrayList<String> {
|
|
|
|
val categories: ArrayList<String> = arrayListOf()
|
|
|
|
if (PreferenceHelper.getBoolean(
|
|
|
|
"intro_category_key",
|
|
|
|
false
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
categories.add("intro")
|
|
|
|
}
|
|
|
|
if (PreferenceHelper.getBoolean(
|
|
|
|
"selfpromo_category_key",
|
|
|
|
false
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
categories.add("selfpromo")
|
|
|
|
}
|
|
|
|
if (PreferenceHelper.getBoolean(
|
|
|
|
"interaction_category_key",
|
|
|
|
false
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
categories.add("interaction")
|
|
|
|
}
|
|
|
|
if (PreferenceHelper.getBoolean(
|
|
|
|
"sponsors_category_key",
|
|
|
|
true
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
categories.add("sponsor")
|
|
|
|
}
|
|
|
|
if (PreferenceHelper.getBoolean(
|
|
|
|
"outro_category_key",
|
|
|
|
false
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
categories.add("outro")
|
|
|
|
}
|
|
|
|
if (PreferenceHelper.getBoolean(
|
|
|
|
"filler_category_key",
|
|
|
|
false
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
categories.add("filler")
|
|
|
|
}
|
|
|
|
if (PreferenceHelper.getBoolean(
|
|
|
|
"music_offtopic_category_key",
|
|
|
|
false
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
categories.add("music_offtopic")
|
|
|
|
}
|
|
|
|
if (PreferenceHelper.getBoolean(
|
|
|
|
"preview_category_key",
|
|
|
|
false
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
categories.add("preview")
|
|
|
|
}
|
|
|
|
return categories
|
|
|
|
}
|
2022-07-17 02:19:32 +05:30
|
|
|
}
|