Improve MPV backend audio track handling

Add proper validation and fallback logic for audio track selection in MPV backend:
- Validate audio track index is within bounds before switching
- Handle streams without separate audio tracks (single asset streams)
- Reset selectedAudioTrackIndex if out of bounds
- Add fallback path for streams without audioTracks array

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Arkadiusz Fal
2025-11-09 14:04:43 +01:00
parent 5b18c3c114
commit 86e843305f

View File

@@ -326,13 +326,28 @@ final class MPVBackend: PlayerBackend {
startPlaying()
}
stream.audioAsset = AVURLAsset(url: stream.audioTracks[stream.selectedAudioTrackIndex].url)
let fileToLoad = self.model.musicMode ? stream.audioAsset.url : stream.videoAsset.url
let audioTrack = self.model.musicMode ? nil : stream.audioAsset.url
// Handle streams with multiple audio tracks
if !stream.audioTracks.isEmpty {
if stream.selectedAudioTrackIndex >= stream.audioTracks.count {
stream.selectedAudioTrackIndex = 0
}
stream.audioAsset = AVURLAsset(url: stream.audioTracks[stream.selectedAudioTrackIndex].url)
let fileToLoad = self.model.musicMode ? stream.audioAsset.url : stream.videoAsset.url
let audioTrack = self.model.musicMode ? nil : stream.audioAsset.url
client.loadFile(fileToLoad, audio: audioTrack, bitrate: stream.bitrate, kind: stream.kind, sub: captions?.url, time: time, forceSeekable: stream.kind == .hls) { [weak self] _ in
self?.isLoadingVideo = true
self?.pause()
client.loadFile(fileToLoad, audio: audioTrack, bitrate: stream.bitrate, kind: stream.kind, sub: captions?.url, time: time, forceSeekable: stream.kind == .hls) { [weak self] _ in
self?.isLoadingVideo = true
self?.pause()
}
} else {
// Fallback for streams without separate audio tracks (e.g., single asset streams)
let fileToLoad = stream.videoAsset.url
client.loadFile(fileToLoad, bitrate: stream.bitrate, kind: stream.kind, sub: captions?.url, time: time, forceSeekable: stream.kind == .hls) { [weak self] _ in
self?.isLoadingVideo = true
self?.pause()
}
}
}
}
@@ -739,6 +754,12 @@ final class MPVBackend: PlayerBackend {
func switchAudioTrack(to index: Int) {
guard let stream, let video else { return }
// Validate the index is within bounds
guard index >= 0 && index < stream.audioTracks.count else {
logger.error("Invalid audio track index: \(index), available tracks: \(stream.audioTracks.count)")
return
}
stream.selectedAudioTrackIndex = index
model.saveTime { [weak self] in