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