fix: persist audio track selection across songs (preferredTrackIndex)

This commit is contained in:
yumoqing 2026-06-21 23:33:08 +08:00
parent bf4cb64286
commit 4e58377582

View File

@ -70,6 +70,9 @@ final class PlayerBridge: ObservableObject {
//
private var cachedDuration: Double = 0
//
private var preferredTrackIndex: Int = 0
// MARK: -
func setup() {
@ -340,14 +343,17 @@ final class PlayerBridge: ObservableObject {
let options = group.options
availableTracks = options.enumerated().map { idx, _ in "Track \(idx + 1)" }
currentTrackIndex = 0
//
if let defaultOpt = group.defaultOption,
let idx = options.firstIndex(of: defaultOpt) {
currentTrackIndex = idx
// 使
var targetIndex = preferredTrackIndex
if targetIndex >= options.count {
targetIndex = 0
}
//
item.select(options[targetIndex], in: group)
currentTrackIndex = targetIndex
updateTrackLabel()
}
}
@ -361,6 +367,7 @@ final class PlayerBridge: ObservableObject {
item.select(group.options[index], in: group)
currentTrackIndex = index
preferredTrackIndex = index //
updateTrackLabel()
}
}