autoselect subtitles

This commit is contained in:
LagradOst 2022-01-08 20:39:22 +01:00
parent 355aff9d3f
commit a194cae245
4 changed files with 104 additions and 33 deletions

View file

@ -10,7 +10,6 @@ import com.google.android.exoplayer2.database.StandaloneDatabaseProvider
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory
import com.google.android.exoplayer2.source.MergingMediaSource
import com.google.android.exoplayer2.source.SingleSampleMediaSource
import com.google.android.exoplayer2.text.Cue
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
import com.google.android.exoplayer2.trackselection.TrackSelector
import com.google.android.exoplayer2.ui.SubtitleView
@ -509,14 +508,6 @@ class CS3IPlayer : IPlayer {
super.onTracksInfoChanged(tracksInfo)
}
override fun onCues(cues: MutableList<Cue>) {
Log.i(TAG, "CUES: ${cues.size}")
if(cues.size > 0) {
Log.i(TAG, "CUES SAY: ${cues.first().text}")
}
super.onCues(cues)
}
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
exoPlayer?.let { exo ->
updateIsPlaying?.invoke(
@ -641,6 +632,7 @@ class CS3IPlayer : IPlayer {
)
subtitleHelper.setActiveSubtitles(activeSubtitles.toSet())
if (simpleCache == null)
simpleCache = getCache(context, cacheSize)
val cacheFactory = CacheDataSource.Factory().apply {

View file

@ -21,10 +21,8 @@ import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.mvvm.observe
import com.lagradost.cloudstream3.ui.player.PlayerSubtitleHelper.Companion.toSubtitleMimeType
import com.lagradost.cloudstream3.ui.result.ResultEpisode
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.ExtractorLink
import com.lagradost.cloudstream3.utils.ExtractorUri
import com.lagradost.cloudstream3.utils.Qualities
import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment
import com.lagradost.cloudstream3.utils.*
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
import com.lagradost.cloudstream3.utils.UIHelper.hideSystemUI
import com.lagradost.cloudstream3.utils.UIHelper.popCurrentPage
@ -52,6 +50,8 @@ class GeneratorPlayer : FullScreenPlayer() {
private var isActive: Boolean = false
private var isNextEpisode: Boolean = false // this is used to reset the watch time
private var preferredAutoSelectSubtitles: String? = null // null means do nothing, "" means none
private fun startLoading() {
player.release()
currentSelectedSubtitles = null
@ -380,6 +380,50 @@ class GeneratorPlayer : FullScreenPlayer() {
}
}
private fun autoSelectFromSettings() {
// auto select subtitle based of settings
val langCode = preferredAutoSelectSubtitles
if (!langCode.isNullOrEmpty() && player.getCurrentPreferredSubtitle() == null) {
val lang = SubtitleHelper.fromTwoLettersToLanguage(langCode) ?: return
currentSubs.firstOrNull { sub ->
sub.name.startsWith(lang)
|| sub.name.trim() == langCode
}?.let { sub ->
context?.let { ctx ->
if (setSubtitles(sub)) {
player.reloadPlayer(ctx)
player.handleEvent(CSPlayerEvent.Play)
}
}
}
}
}
private fun autoSelectFromDownloads() {
if (player.getCurrentPreferredSubtitle() == null) {
currentSubs.firstOrNull { sub ->
(sub.origin == SubtitleOrigin.DOWNLOADED_FILE || sub.name == context?.getString(
R.string.default_subtitles
))
}?.let { sub ->
context?.let { ctx ->
if (setSubtitles(sub)) {
player.reloadPlayer(ctx)
player.handleEvent(CSPlayerEvent.Play)
}
}
}
}
}
private fun autoSelectSubtitles() {
normalSafeApiCall {
autoSelectFromSettings()
autoSelectFromDownloads()
}
}
@SuppressLint("SetTextI18n")
fun setTitle() {
var headerName: String? = null
@ -445,6 +489,8 @@ class GeneratorPlayer : FullScreenPlayer() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
preferredAutoSelectSubtitles = SubtitlesFragment.getAutoSelectLanguageISO639_1()
if (currentSelectedLink == null) {
viewModel.loadLinks()
}
@ -482,9 +528,11 @@ class GeneratorPlayer : FullScreenPlayer() {
overlay_loading_skip_button?.isVisible = it.isNotEmpty()
}
observe(viewModel.currentSubs) {
currentSubs = it
player.setActiveSubtitles(it)
observe(viewModel.currentSubs) { set ->
currentSubs = set
player.setActiveSubtitles(set)
autoSelectSubtitles()
}
}
}

View file

@ -97,7 +97,7 @@ class PlayerGeneratorViewModel : ViewModel() {
_currentLinks.postValue(currentLinks)
}, {
currentSubs.add(it)
_currentSubs.postValue(currentSubs)
// _currentSubs.postValue(currentSubs) // this causes ConcurrentModificationException, so fuck it
})
}

View file

@ -64,8 +64,15 @@ class SubtitlesFragment : Fragment() {
fun Context.fromSaveToStyle(data: SaveCaptionStyle): CaptionStyleCompat {
val typeface = data.typeface
return CaptionStyleCompat(
data.foregroundColor, data.backgroundColor, data.windowColor, data.edgeType, data.edgeColor,
if (typeface == null) Typeface.SANS_SERIF else ResourcesCompat.getFont(this, typeface)
data.foregroundColor,
data.backgroundColor,
data.windowColor,
data.edgeType,
data.edgeColor,
if (typeface == null) Typeface.SANS_SERIF else ResourcesCompat.getFont(
this,
typeface
)
)
}
@ -256,11 +263,26 @@ class SubtitlesFragment : Fragment() {
subs_edge_type.setOnClickListener { textView ->
val edgeTypes = listOf(
Pair(CaptionStyleCompat.EDGE_TYPE_NONE, textView.context.getString(R.string.subtitles_none)),
Pair(CaptionStyleCompat.EDGE_TYPE_OUTLINE, textView.context.getString(R.string.subtitles_outline)),
Pair(CaptionStyleCompat.EDGE_TYPE_DEPRESSED, textView.context.getString(R.string.subtitles_depressed)),
Pair(CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW, textView.context.getString(R.string.subtitles_shadow)),
Pair(CaptionStyleCompat.EDGE_TYPE_RAISED, textView.context.getString(R.string.subtitles_raised)),
Pair(
CaptionStyleCompat.EDGE_TYPE_NONE,
textView.context.getString(R.string.subtitles_none)
),
Pair(
CaptionStyleCompat.EDGE_TYPE_OUTLINE,
textView.context.getString(R.string.subtitles_outline)
),
Pair(
CaptionStyleCompat.EDGE_TYPE_DEPRESSED,
textView.context.getString(R.string.subtitles_depressed)
),
Pair(
CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW,
textView.context.getString(R.string.subtitles_shadow)
),
Pair(
CaptionStyleCompat.EDGE_TYPE_RAISED,
textView.context.getString(R.string.subtitles_raised)
),
)
//showBottomDialog
@ -375,7 +397,15 @@ class SubtitlesFragment : Fragment() {
subs_auto_select_language.setOnClickListener { textView ->
val langMap = arrayListOf(
SubtitleHelper.Language639("None", "None", "", "", "", "", ""),
SubtitleHelper.Language639(
textView.context.getString(R.string.none),
textView.context.getString(R.string.none),
"",
"",
"",
"",
""
),
)
langMap.addAll(SubtitleHelper.languages)
@ -438,7 +468,8 @@ class SubtitlesFragment : Fragment() {
getPixels(TypedValue.COMPLEX_UNIT_SP, 25.0f).toFloat(),
Cue.TEXT_SIZE_TYPE_ABSOLUTE
)
.setText(subtitle_text.context.getString(R.string.subtitles_example_text)).build()
.setText(subtitle_text.context.getString(R.string.subtitles_example_text))
.build()
)
)
}