mirror of
https://github.com/yattee/yattee.git
synced 2025-12-03 22:58:16 +00:00
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:
@@ -326,6 +326,12 @@ final class MPVBackend: PlayerBackend {
|
|||||||
startPlaying()
|
startPlaying()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
stream.audioAsset = AVURLAsset(url: stream.audioTracks[stream.selectedAudioTrackIndex].url)
|
||||||
let fileToLoad = self.model.musicMode ? stream.audioAsset.url : stream.videoAsset.url
|
let fileToLoad = self.model.musicMode ? stream.audioAsset.url : stream.videoAsset.url
|
||||||
let audioTrack = self.model.musicMode ? nil : stream.audioAsset.url
|
let audioTrack = self.model.musicMode ? nil : stream.audioAsset.url
|
||||||
@@ -334,6 +340,15 @@ final class MPVBackend: PlayerBackend {
|
|||||||
self?.isLoadingVideo = true
|
self?.isLoadingVideo = true
|
||||||
self?.pause()
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -740,6 +755,12 @@ final class MPVBackend: PlayerBackend {
|
|||||||
func switchAudioTrack(to index: Int) {
|
func switchAudioTrack(to index: Int) {
|
||||||
guard let stream, let video else { return }
|
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
|
stream.selectedAudioTrackIndex = index
|
||||||
model.saveTime { [weak self] in
|
model.saveTime { [weak self] in
|
||||||
self?.playStream(stream, of: video, preservingTime: true, upgrading: false)
|
self?.playStream(stream, of: video, preservingTime: true, upgrading: false)
|
||||||
|
|||||||
Reference in New Issue
Block a user