LibreTube/app/src/main/java/com/github/libretube/util/PlayerHelper.kt

276 lines
8.1 KiB
Kotlin
Raw Normal View History

2022-07-17 01:01:15 +05:30
package com.github.libretube.util
import android.content.Context
import android.content.pm.ActivityInfo
2022-07-17 01:01:15 +05:30
import android.view.accessibility.CaptioningManager
2022-09-20 23:30:51 +05:30
import com.github.libretube.constants.PreferenceKeys
2022-07-17 01:01:15 +05:30
import com.google.android.exoplayer2.ui.CaptionStyleCompat
import com.google.android.exoplayer2.video.VideoSize
2022-07-17 01:01:15 +05:30
object PlayerHelper {
2022-07-24 16:29:15 +05:30
// get the audio source following the users preferences
2022-09-22 21:29:15 +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
}
fun getOrientation(videoSize: VideoSize): Int {
val fullscreenOrientationPref = PreferenceHelper.getString(
PreferenceKeys.FULLSCREEN_ORIENTATION,
"ratio"
)
return when (fullscreenOrientationPref) {
"ratio" -> {
// probably a youtube shorts video
if (videoSize.height > videoSize.width) {
ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
} // a video with normal aspect ratio
else {
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}
}
"auto" -> ActivityInfo.SCREEN_ORIENTATION_SENSOR
"landscape" -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
"portrait" -> ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
else -> ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}
}
2022-10-07 23:00:59 +05:30
val autoRotationEnabled: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.AUTO_FULLSCREEN,
false
)
val relatedStreamsEnabled: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.RELATED_STREAMS,
true
)
val pausePlayerOnScreenOffEnabled: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.PAUSE_ON_SCREEN_OFF,
false
)
val watchPositionsEnabled: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.WATCH_POSITION_TOGGLE,
true
)
val watchHistoryEnabled: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.WATCH_HISTORY_TOGGLE,
true
)
val useSystemCaptionStyle: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.SYSTEM_CAPTION_STYLE,
true
)
val videoFormatPreference: String
get() = PreferenceHelper.getString(
PreferenceKeys.PLAYER_VIDEO_FORMAT,
"webm"
)
val bufferingGoal: Int
get() = PreferenceHelper.getString(
PreferenceKeys.BUFFERING_GOAL,
"50"
).toInt() * 1000
val sponsorBlockEnabled: Boolean
get() = PreferenceHelper.getBoolean(
"sb_enabled_key",
true
)
val sponsorBlockNotifications: Boolean
get() = PreferenceHelper.getBoolean(
"sb_notifications_key",
true
)
val defaultSubtitleCode: String
get() {
val code = PreferenceHelper.getString(
PreferenceKeys.DEFAULT_SUBTITLE,
""
)
if (code.contains("-")) {
return code.split("-")[0]
}
return code
}
val skipButtonsEnabled: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.SKIP_BUTTONS,
false
)
val pipEnabled: Boolean get() = PreferenceHelper.getBoolean(
PreferenceKeys.PICTURE_IN_PICTURE,
true
)
val skipSegmentsManually: Boolean
get() = PreferenceHelper.getBoolean(
PreferenceKeys.SB_SKIP_MANUALLY,
false
)
val progressiveLoadingIntervalSize: String
get() = PreferenceHelper.getString(
PreferenceKeys.PROGRESSIVE_LOADING_INTERVAL_SIZE,
"64"
)
fun getDefaultResolution(context: Context): String {
return if (NetworkHelper.isNetworkMobile(context)) {
PreferenceHelper.getString(
PreferenceKeys.DEFAULT_RESOLUTION_MOBILE,
""
)
} else {
PreferenceHelper.getString(
PreferenceKeys.DEFAULT_RESOLUTION,
""
)
}
}
2022-07-17 02:19:32 +05:30
}