diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/player/PlayerScreen.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/player/PlayerScreen.kt index 2ed5767ef..641ba2ea6 100644 --- a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/player/PlayerScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/player/PlayerScreen.kt @@ -312,6 +312,28 @@ fun PlayerScreen( } } + LaunchedEffect( + playerController, + playbackSnapshot.isLoading, + preferredAudioSelectionApplied, + preferredSubtitleSelectionApplied, + ) { + if (playerController == null || playbackSnapshot.isLoading) { + return@LaunchedEffect + } + if (preferredAudioSelectionApplied && preferredSubtitleSelectionApplied) { + return@LaunchedEffect + } + + repeat(10) { + refreshTracks() + if (preferredAudioSelectionApplied && preferredSubtitleSelectionApplied) { + return@LaunchedEffect + } + delay(300) + } + } + LaunchedEffect(playerController, playbackSnapshot.isLoading, initialPositionMs, initialSeekApplied) { val controller = playerController ?: return@LaunchedEffect if (initialSeekApplied || playbackSnapshot.isLoading || initialPositionMs <= 0L) { diff --git a/composeApp/src/iosMain/kotlin/com/nuvio/app/features/player/PlayerEngine.ios.kt b/composeApp/src/iosMain/kotlin/com/nuvio/app/features/player/PlayerEngine.ios.kt index 9a9d234f2..34d3be8a1 100644 --- a/composeApp/src/iosMain/kotlin/com/nuvio/app/features/player/PlayerEngine.ios.kt +++ b/composeApp/src/iosMain/kotlin/com/nuvio/app/features/player/PlayerEngine.ios.kt @@ -104,10 +104,25 @@ actual fun PlatformPlayerSurface( } override fun selectAudioTrack(index: Int) { - // Convert from list index to mpv track id + // Convert from logical track index to mpv track id val count = bridge.getAudioTrackCount() - if (index in 0 until count) { - val trackId = bridge.getAudioTrackId(index).toIntOrNull() ?: (index + 1) + if (count <= 0) return + + val trackId = (0 until count) + .firstNotNullOfOrNull { at -> + if (bridge.getAudioTrackIndex(at) == index) { + bridge.getAudioTrackId(at).toIntOrNull() + } else { + null + } + } + ?: if (index in 0 until count) { + bridge.getAudioTrackId(index).toIntOrNull() ?: (index + 1) + } else { + null + } + + if (trackId != null) { bridge.selectAudioTrack(trackId) } } @@ -117,8 +132,23 @@ actual fun PlatformPlayerSurface( bridge.selectSubtitleTrack(-1) // disable } else { val count = bridge.getSubtitleTrackCount() - if (index in 0 until count) { - val trackId = bridge.getSubtitleTrackId(index).toIntOrNull() ?: (index + 1) + if (count <= 0) return + + val trackId = (0 until count) + .firstNotNullOfOrNull { at -> + if (bridge.getSubtitleTrackIndex(at) == index) { + bridge.getSubtitleTrackId(at).toIntOrNull() + } else { + null + } + } + ?: if (index in 0 until count) { + bridge.getSubtitleTrackId(index).toIntOrNull() ?: (index + 1) + } else { + null + } + + if (trackId != null) { bridge.selectSubtitleTrack(trackId) } } @@ -134,11 +164,27 @@ actual fun PlatformPlayerSurface( } override fun clearExternalSubtitleAndSelect(trackIndex: Int) { - val trackId = if (trackIndex < 0) -1 else { + val trackId = if (trackIndex < 0) { + -1 + } else { val count = bridge.getSubtitleTrackCount() - if (trackIndex in 0 until count) { - bridge.getSubtitleTrackId(trackIndex).toIntOrNull() ?: (trackIndex + 1) - } else trackIndex + 1 + if (count <= 0) { + trackIndex + 1 + } else { + (0 until count) + .firstNotNullOfOrNull { at -> + if (bridge.getSubtitleTrackIndex(at) == trackIndex) { + bridge.getSubtitleTrackId(at).toIntOrNull() + } else { + null + } + } + ?: if (trackIndex in 0 until count) { + bridge.getSubtitleTrackId(trackIndex).toIntOrNull() ?: (trackIndex + 1) + } else { + trackIndex + 1 + } + } } bridge.clearExternalSubtitleAndSelect(trackId) }