fix(mpv): apply carried audio/subtitle track after file load

Setting sid/aid before loadfile does not stick for embedded tracks, so a
carried-over subtitle was silently dropped on a freshly loaded episode.
Apply the initial audio/subtitle selection in the MPV_EVENT_FILE_LOADED
handler (after tracks are enumerated) for embedded and external alike, on
both iOS and Android.
This commit is contained in:
Fredrik Burmester
2026-05-30 20:15:37 +02:00
parent 6876ce046f
commit d2e73021b1
2 changed files with 27 additions and 12 deletions

View File

@@ -684,11 +684,19 @@ class MPVLayerRenderer(private val context: Context) : MPVLib.EventObserver {
MPVLib.command(arrayOf("sub-add", subUrl, "auto")) MPVLib.command(arrayOf("sub-add", subUrl, "auto"))
} }
pendingExternalSubtitles = emptyList() pendingExternalSubtitles = emptyList()
// Set subtitle after external subs are added
initialSubtitleId?.let { setSubtitleTrack(it) } ?: disableSubtitles()
} }
// Apply the initial audio/subtitle selection now that the file's
// tracks are enumerated. Setting sid/aid before `loadfile` does not
// reliably stick for embedded tracks (the selection is silently
// dropped), so we (re)apply here for embedded and external alike.
// This is what makes a carried-over subtitle show up on the next
// episode without a manual re-selection.
if (initialAudioId != null && initialAudioId > 0) {
setAudioTrack(initialAudioId)
}
initialSubtitleId?.let { setSubtitleTrack(it) } ?: disableSubtitles()
if (!isReadyToSeek) { if (!isReadyToSeek) {
isReadyToSeek = true isReadyToSeek = true
mainHandler.post { delegate?.onReadyToSeek() } mainHandler.post { delegate?.onReadyToSeek() }

View File

@@ -485,8 +485,7 @@ final class MPVLayerRenderer {
switch event.event_id { switch event.event_id {
case MPV_EVENT_FILE_LOADED: case MPV_EVENT_FILE_LOADED:
// Add external subtitles now that the file is loaded // Add external subtitles now that the file is loaded
let hadExternalSubs = !pendingExternalSubtitles.isEmpty if !pendingExternalSubtitles.isEmpty, let handle = mpv {
if hadExternalSubs, let handle = mpv {
for (index, subUrl) in pendingExternalSubtitles.enumerated() { for (index, subUrl) in pendingExternalSubtitles.enumerated() {
print("🔧 Adding external subtitle [\(index)]: \(subUrl)") print("🔧 Adding external subtitle [\(index)]: \(subUrl)")
// Use commandSync to ensure subs are added in exact order (not async) // Use commandSync to ensure subs are added in exact order (not async)
@@ -494,12 +493,20 @@ final class MPVLayerRenderer {
commandSync(handle, ["sub-add", subUrl, "auto"]) commandSync(handle, ["sub-add", subUrl, "auto"])
} }
pendingExternalSubtitles = [] pendingExternalSubtitles = []
// Set subtitle after external subs are added }
if let subId = initialSubtitleId { // Apply the initial audio/subtitle selection now that the file's
setSubtitleTrack(subId) // tracks are enumerated. Setting sid/aid before `loadfile` does not
} else { // reliably stick for embedded tracks (the selection is silently
disableSubtitles() // dropped), so we (re)apply here for embedded and external alike.
} // This is what makes a carried-over subtitle show up on the next
// episode without a manual re-selection.
if let audioId = initialAudioId, audioId > 0 {
setAudioTrack(audioId)
}
if let subId = initialSubtitleId {
setSubtitleTrack(subId)
} else {
disableSubtitles()
} }
if !isReadyToSeek { if !isReadyToSeek {
isReadyToSeek = true isReadyToSeek = true