Fix switching audio/subtitle tracks during playback (#121)

An error would occur when switching audio or subtitle tracks during
playback, but would be ignored. This meant the tracks can't be changed.

#111 was too aggressive on switching dispatchers. The player can only be
accessed on `Main`, so this PR fixes that.
This commit is contained in:
damontecres 2025-10-30 19:24:53 -04:00 committed by GitHub
parent 982506555f
commit c65831cb05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -56,6 +56,7 @@ import com.github.damontecres.wholphin.util.subtitleMimeTypes
import com.github.damontecres.wholphin.util.supportItemKinds import com.github.damontecres.wholphin.util.supportItemKinds
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
@ -590,7 +591,7 @@ class PlaybackViewModel
currentItemPlayback.value!!, currentItemPlayback.value!!,
index, index,
currentItemPlayback.value?.subtitleIndex, currentItemPlayback.value?.subtitleIndex,
player.currentPosition, onMain { player.currentPosition },
true, true,
) )
} }
@ -603,7 +604,7 @@ class PlaybackViewModel
currentItemPlayback.value!!, currentItemPlayback.value!!,
currentItemPlayback.value?.audioIndex, currentItemPlayback.value?.audioIndex,
index, index,
player.currentPosition, onMain { player.currentPosition },
true, true,
) )
} }
@ -656,7 +657,7 @@ class PlaybackViewModel
while (isActive) { while (isActive) {
delay(500L) delay(500L)
val currentTicks = val currentTicks =
withContext(Dispatchers.Main) { player.currentPosition.milliseconds.inWholeTicks } onMain { player.currentPosition.milliseconds.inWholeTicks }
val currentSegment = val currentSegment =
segments.items segments.items
.firstOrNull { .firstOrNull {
@ -956,6 +957,8 @@ val Format.idAsInt: Int?
} }
} }
suspend fun <T> onMain(block: suspend CoroutineScope.() -> T) = withContext(Dispatchers.Main, block)
@OptIn(UnstableApi::class) @OptIn(UnstableApi::class)
private fun applyTrackSelections( private fun applyTrackSelections(
player: Player, player: Player,