Save selected subtitle language (#765)

This commit is contained in:
self-similarity 2023-11-12 15:36:21 +00:00 committed by GitHub
parent 5bf2b4ead2
commit e11d36aed8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 12 deletions

View file

@ -1014,7 +1014,8 @@ class CS3IPlayer : IPlayer {
format.id!!,
SubtitleOrigin.EMBEDDED_IN_VIDEO,
format.sampleMimeType ?: MimeTypes.APPLICATION_SUBRIP,
emptyMap()
emptyMap(),
format.language
)
}

View file

@ -100,7 +100,8 @@ class DownloadFileGenerator(
uri.toString(),
SubtitleOrigin.DOWNLOADED_FILE,
name.toSubtitleMimeType(),
emptyMap()
emptyMap(),
null
)
)
}

View file

@ -23,6 +23,7 @@ import androidx.media3.common.Format.NO_VALUE
import androidx.media3.common.MimeTypes
import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull
import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
import com.lagradost.cloudstream3.CommonActivity.showToast
import com.lagradost.cloudstream3.databinding.DialogOnlineSubtitlesBinding
import com.lagradost.cloudstream3.databinding.FragmentPlayerBinding
@ -38,6 +39,7 @@ import com.lagradost.cloudstream3.ui.player.source_priority.QualityDataHelper
import com.lagradost.cloudstream3.ui.player.source_priority.QualityProfileDialog
import com.lagradost.cloudstream3.ui.result.*
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings
import com.lagradost.cloudstream3.ui.subtitles.SUBTITLE_AUTO_SELECT_KEY
import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment.Companion.getAutoSelectLanguageISO639_1
import com.lagradost.cloudstream3.utils.*
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
@ -100,10 +102,33 @@ class GeneratorPlayer : FullScreenPlayer() {
binding?.playerLoadingOverlay?.isVisible = true
}
private fun setSubtitles(sub: SubtitleData?): Boolean {
currentSelectedSubtitles = sub
//Log.i(TAG, "setSubtitles = $sub")
return player.setPreferredSubtitles(sub)
private fun setSubtitles(subtitle: SubtitleData?): Boolean {
// If subtitle is changed -> Save the language
if (subtitle != currentSelectedSubtitles) {
val subtitleLanguage639 = if (subtitle == null) {
// "" is No Subtitles
""
} else if (subtitle.languageCode != null) {
// Could be "English 4" which is why it is trimmed.
val trimmedLanguage = subtitle.languageCode.replace(Regex("\\d"), "").trim()
languages.firstOrNull { language ->
language.languageName.equals(trimmedLanguage, ignoreCase = true) ||
language.ISO_639_1 == subtitle.languageCode
}?.ISO_639_1
} else {
null
}
if (subtitleLanguage639 != null) {
setKey(SUBTITLE_AUTO_SELECT_KEY, subtitleLanguage639)
preferredAutoSelectSubtitles = subtitleLanguage639
}
}
currentSelectedSubtitles = subtitle
//Log.i(TAG, "setSubtitles = $subtitle")
return player.setPreferredSubtitles(subtitle)
}
override fun embeddedSubtitlesFetched(subtitles: List<SubtitleData>) {
@ -448,7 +473,8 @@ class GeneratorPlayer : FullScreenPlayer() {
url = url,
origin = SubtitleOrigin.URL,
mimeType = url.toSubtitleMimeType(),
headers = currentSubtitle.headers
headers = currentSubtitle.headers,
currentSubtitle.lang
)
runOnMainThread {
addAndSelectSubtitles(subtitle)
@ -536,7 +562,8 @@ class GeneratorPlayer : FullScreenPlayer() {
uri.toString(),
SubtitleOrigin.DOWNLOADED_FILE,
name.toSubtitleMimeType(),
emptyMap()
emptyMap(),
null
)
addAndSelectSubtitles(subtitleData)
@ -946,7 +973,7 @@ class GeneratorPlayer : FullScreenPlayer() {
var maxEpisodeSet: Int? = null
var hasRequestedStamps: Boolean = false
override fun playerPositionChanged(position: Long, duration : Long) {
override fun playerPositionChanged(position: Long, duration: Long) {
// Don't save livestream data
if ((currentMeta as? ResultEpisode)?.tvType?.isLiveStream() == true) return
@ -1209,7 +1236,7 @@ class GeneratorPlayer : FullScreenPlayer() {
}
}
override fun playerDimensionsLoaded(width: Int, height : Int) {
override fun playerDimensionsLoaded(width: Int, height: Int) {
setPlayerDimen(width to height)
}

View file

@ -30,13 +30,15 @@ enum class SubtitleOrigin {
* @param name To be displayed in the player
* @param url Url for the subtitle, when EMBEDDED_IN_VIDEO this variable is used as the real backend id
* @param headers if empty it will use the base onlineDataSource headers else only the specified headers
* @param languageCode Not guaranteed to follow any standard. Could be something like "English 4" or "en".
* */
data class SubtitleData(
val name: String,
val url: String,
val origin: SubtitleOrigin,
val mimeType: String,
val headers: Map<String, String>
val headers: Map<String, String>,
val languageCode: String?
) {
/** Internal ID for exoplayer, unique for each link*/
fun getId(): String {
@ -80,7 +82,8 @@ class PlayerSubtitleHelper {
url = subtitleFile.url,
origin = SubtitleOrigin.URL,
mimeType = subtitleFile.url.toSubtitleMimeType(),
headers = emptyMap()
headers = emptyMap(),
languageCode = subtitleFile.lang
)
}
}