Skip to content

Commit

Permalink
fix(player-ux): fix player forced preferred subtitle mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
rhenwinch committed Sep 25, 2024
1 parent 8331a61 commit 606d516
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
import androidx.media3.session.MediaSession
import androidx.media3.ui.CaptionStyleCompat
import androidx.media3.ui.SubtitleView
import com.flixclusive.core.locale.UiText
import com.flixclusive.core.ui.player.renderer.CustomTextRenderer
import com.flixclusive.core.ui.player.util.MimeTypeParser
import com.flixclusive.core.ui.player.util.MimeTypeParser.toMimeType
Expand All @@ -49,7 +50,6 @@ import com.flixclusive.core.ui.player.util.getCacheFactory
import com.flixclusive.core.ui.player.util.getLoadControl
import com.flixclusive.core.ui.player.util.getRenderers
import com.flixclusive.core.ui.player.util.handleError
import com.flixclusive.core.locale.UiText
import com.flixclusive.core.util.exception.safeCall
import com.flixclusive.core.util.log.errorLog
import com.flixclusive.core.util.log.infoLog
Expand Down Expand Up @@ -102,6 +102,9 @@ class FlixclusivePlayerManager(
private val showErrorCallback: (message: UiText) -> Unit
) : Player.Listener {
private var mediaSession: MediaSession? = null
private var currentStreamPlaying: Stream? = null
private var areTracksInitialized: Boolean = false

var player: ExoPlayer? by mutableStateOf(null)
private set
var hasBeenInitialized by mutableStateOf(false)
Expand Down Expand Up @@ -185,24 +188,6 @@ class FlixclusivePlayerManager(
}
}

override fun onTracksChanged(tracks: Tracks) {
extractAudios()
extractEmbeddedSubtitles()

selectedSubtitleIndex = when {
!appSettings.isSubtitleEnabled -> 0 // == Off subtitles
else -> availableSubtitles.getIndexOfPreferredLanguage(
preferredLanguage = preferredSubtitleLanguage,
languageExtractor = { it.language }
)
}

selectedAudioIndex = availableAudios.getIndexOfPreferredLanguage(
preferredLanguage = preferredAudioLanguage,
languageExtractor = { it }
)
}

override fun onAudioSessionIdChanged(audioSessionId: Int) {
super.onAudioSessionIdChanged(audioSessionId)
volumeManager.loudnessEnhancer?.release()
Expand Down Expand Up @@ -289,6 +274,11 @@ class FlixclusivePlayerManager(
player?.run {
infoLog("Preparing the player...")

if (link != currentStreamPlaying) {
currentStreamPlaying = link
areTracksInitialized = false
}

val mediaItem = createMediaItem(
url = link.url,
title = title
Expand Down Expand Up @@ -332,24 +322,30 @@ class FlixclusivePlayerManager(

private fun onReady() {
player?.run {
extractAudios()
extractEmbeddedSubtitles()

selectedSubtitleIndex = when {
!appSettings.isSubtitleEnabled -> 0 // == Off subtitles
else -> availableSubtitles.getIndexOfPreferredLanguage(
preferredLanguage = preferredSubtitleLanguage,
languageExtractor = { it.language }
if (!areTracksInitialized) {
extractAudios()
extractEmbeddedSubtitles()

val subtitleIndex = when {
!appSettings.isSubtitleEnabled -> 0 // == Off subtitles
else -> availableSubtitles.getIndexOfPreferredLanguage(
preferredLanguage = preferredSubtitleLanguage,
languageExtractor = { it.language }
)
}
val audioIndex = availableAudios.getIndexOfPreferredLanguage(
preferredLanguage = preferredAudioLanguage,
languageExtractor = { it }
)

onSubtitleChange(index = subtitleIndex)
onAudioChange(index = audioIndex)

areTracksInitialized = true
}
selectedAudioIndex = availableAudios.getIndexOfPreferredLanguage(
preferredLanguage = preferredAudioLanguage,
languageExtractor = { it }
)

setPlaybackSpeed(playbackSpeed)
currentTextRenderer?.setRenderOffsetMs(offset = subtitleOffset)
onSubtitleChange(index = selectedSubtitleIndex)
onAudioChange(index = selectedAudioIndex)

hasBeenInitialized = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ import androidx.lifecycle.LifecycleEventObserver
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.ui.AspectRatioFrameLayout
import com.flixclusive.core.locale.UiText
import com.flixclusive.core.ui.player.FlixclusivePlayerManager
import com.flixclusive.core.ui.player.PlayerSnackbarMessageType
import com.flixclusive.core.ui.player.R
import com.flixclusive.core.ui.player.databinding.CustomPlayerBinding
import com.flixclusive.core.locale.UiText
import com.flixclusive.model.film.util.FilmType
import com.flixclusive.domain.provider.CachedLinks
import com.flixclusive.model.database.util.calculateRemainingTime
import com.flixclusive.model.database.util.isTimeInRangeOfThreshold
import com.flixclusive.model.datastore.player.ResizeMode
import com.flixclusive.domain.provider.CachedLinks
import com.flixclusive.model.provider.link.Stream
import com.flixclusive.model.provider.link.Subtitle
import com.flixclusive.model.film.Film
import com.flixclusive.model.film.common.tv.Episode
import com.flixclusive.model.film.util.FilmType
import com.flixclusive.model.provider.link.Stream
import com.flixclusive.model.provider.link.Subtitle
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import java.util.Locale
Expand Down

0 comments on commit 606d516

Please sign in to comment.