From 7e9bc35e61ae549c13a4393ea0ea8dcd9ff1432e Mon Sep 17 00:00:00 2001 From: dirtydishes Date: Mon, 25 May 2026 11:01:51 -0400 Subject: [PATCH] add native audio track selection --- .beads/interactions.jsonl | 1 + Dreamio/NativePlaybackBackend.swift | 4 + Dreamio/NativePlayerViewController.swift | 97 +++- Dreamio/StreamCandidate.swift | 8 + Dreamio/VLCNativePlaybackBackend.swift | 43 ++ ...2026-05-25-native-player-audio-tracks.html | 421 ++++++++++++++++++ 6 files changed, 567 insertions(+), 7 deletions(-) create mode 100644 docs/turns/2026-05-25-native-player-audio-tracks.html diff --git a/.beads/interactions.jsonl b/.beads/interactions.jsonl index 072f8e3..2d57e16 100644 --- a/.beads/interactions.jsonl +++ b/.beads/interactions.jsonl @@ -23,6 +23,7 @@ {"id":"int-4e095d3f","kind":"field_change","created_at":"2026-05-25T14:38:21.968713Z","actor":"dirtydishes","issue_id":"dreamio-djc","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Auto-select the first discovered VLC subtitle track when playback is still disabled, while preserving manual caption choices."}} {"id":"int-96629c65","kind":"field_change","created_at":"2026-05-25T14:45:38.521113Z","actor":"dirtydishes","issue_id":"dreamio-ppj","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Re-applied the auto-selected VLC subtitle track after stream discovery and playback state changes to harden rendering timing."}} {"id":"int-027cec57","kind":"field_change","created_at":"2026-05-25T14:51:44.599319Z","actor":"dirtydishes","issue_id":"dreamio-3xi","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Captured OpenSubtitles V3 subtitle URLs from browser track elements and textTracks so they can be forwarded to native playback."}} +{"id":"int-8f943c34","kind":"field_change","created_at":"2026-05-25T15:01:35.610049Z","actor":"dirtydishes","issue_id":"dreamio-bao","extra":{"field":"status","new_value":"closed","old_value":"open","reason":"Implemented native audio track discovery and selection with a far-left audio menu in the VLC-backed player."}} {"id":"int-3acaadff","kind":"field_change","created_at":"2026-05-25T15:09:02.023077Z","actor":"dirtydishes","issue_id":"dreamio-h5n","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Limited VLC auto-subtitle reapply to real selection recovery while keeping bounded delayed startup confirmations."}} {"id":"int-c526b5ae","kind":"field_change","created_at":"2026-05-25T15:32:37.748454Z","actor":"dirtydishes","issue_id":"dreamio-dow","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Implemented stream-keyed subtitle buffering, OpenSubtitles parser/resolver hardening, VLC refresh behavior, and focused validation."}} {"id":"int-320e7321","kind":"field_change","created_at":"2026-05-25T15:53:52.866657Z","actor":"dirtydishes","issue_id":"dreamio-hzj","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Hardened OpenSubtitles candidate discovery, nested payload resolution, VLC external subtitle visibility selection, diagnostics, tests, and turn documentation."}} diff --git a/Dreamio/NativePlaybackBackend.swift b/Dreamio/NativePlaybackBackend.swift index 0648eb0..90da71e 100644 --- a/Dreamio/NativePlaybackBackend.swift +++ b/Dreamio/NativePlaybackBackend.swift @@ -6,12 +6,15 @@ protocol NativePlaybackBackend: AnyObject { var onFailure: ((Error) -> Void)? { get set } var onStateChange: (() -> Void)? { get set } var onSubtitleTracksChange: (() -> Void)? { get set } + var onAudioTracksChange: (() -> Void)? { get set } var isPlaying: Bool { get } var isSeekable: Bool { get } var duration: TimeInterval { get } var currentTime: TimeInterval { get } var remainingTime: TimeInterval { get } var position: Float { get } + var audioTracks: [AudioTrack] { get } + var selectedAudioTrackID: Int32 { get } var subtitleTracks: [SubtitleTrack] { get } var selectedSubtitleTrackID: Int32 { get } var subtitleDelay: TimeInterval { get } @@ -23,6 +26,7 @@ protocol NativePlaybackBackend: AnyObject { func togglePlayPause() func seek(to position: Float) func jump(by seconds: TimeInterval) + func selectAudioTrack(id: Int32) func selectSubtitleTrack(id: Int32) func adjustSubtitleDelay(by seconds: TimeInterval) @discardableResult diff --git a/Dreamio/NativePlayerViewController.swift b/Dreamio/NativePlayerViewController.swift index e821aea..1679f1f 100644 --- a/Dreamio/NativePlayerViewController.swift +++ b/Dreamio/NativePlayerViewController.swift @@ -9,6 +9,7 @@ final class NativePlayerViewController: UIViewController { private var progressTimer: Timer? private var isScrubbing = false private var attachedSubtitleURLs: Set + private var audioMenuSignature: String? private var captionsMenuSignature: String? var onDismiss: (() -> Void)? @@ -34,8 +35,11 @@ final class NativePlayerViewController: UIViewController { private let controlsContainer: UIVisualEffectView = { let view = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterialDark)) view.translatesAutoresizingMaskIntoConstraints = false - view.layer.cornerRadius = 16 + view.layer.cornerRadius = 22 view.clipsToBounds = true + view.backgroundColor = UIColor.white.withAlphaComponent(0.08) + view.layer.borderColor = UIColor.white.withAlphaComponent(0.18).cgColor + view.layer.borderWidth = 1 return view }() @@ -49,6 +53,7 @@ final class NativePlayerViewController: UIViewController { private let playPauseButton = NativePlayerViewController.iconButton(systemName: "pause.fill", label: "Play or Pause") private let jumpBackButton = NativePlayerViewController.iconButton(systemName: "gobackward.15", label: "Jump Back 15 Seconds") private let jumpForwardButton = NativePlayerViewController.iconButton(systemName: "goforward.15", label: "Jump Forward 15 Seconds") + private let audioButton = NativePlayerViewController.iconButton(systemName: "waveform.circle", label: "Audio Tracks") private let captionsButton = NativePlayerViewController.iconButton(systemName: "captions.bubble", label: "Captions") private let elapsedLabel: UILabel = { @@ -229,6 +234,11 @@ final class NativePlayerViewController: UIViewController { self?.refreshControls() } } + backend.onAudioTracksChange = { [weak self] in + DispatchQueue.main.async { + self?.refreshControls() + } + } } private func startStartupTimer() { @@ -250,8 +260,9 @@ final class NativePlayerViewController: UIViewController { playPauseButton.addTarget(self, action: #selector(togglePlayPause), for: .touchUpInside) jumpBackButton.addTarget(self, action: #selector(jumpBack), for: .touchUpInside) jumpForwardButton.addTarget(self, action: #selector(jumpForward), for: .touchUpInside) + audioButton.showsMenuAsPrimaryAction = true captionsButton.showsMenuAsPrimaryAction = true - playPauseButton.layer.cornerRadius = 21 + playPauseButton.layer.cornerRadius = 24 scrubber.addTarget(self, action: #selector(scrubbingStarted), for: .touchDown) scrubber.addTarget(self, action: #selector(scrubberChanged), for: .valueChanged) scrubber.addTarget(self, action: #selector(scrubbingEnded), for: [.touchUpInside, .touchUpOutside, .touchCancel]) @@ -266,12 +277,19 @@ final class NativePlayerViewController: UIViewController { timeAndScrubRow.alignment = .center timeAndScrubRow.spacing = 8 - let controlRow = UIStackView(arrangedSubviews: [jumpBackButton, playPauseButton, jumpForwardButton, captionsButton]) + let playbackCluster = UIStackView(arrangedSubviews: [jumpBackButton, playPauseButton, jumpForwardButton]) + playbackCluster.translatesAutoresizingMaskIntoConstraints = false + playbackCluster.axis = .horizontal + playbackCluster.alignment = .center + playbackCluster.distribution = .equalCentering + playbackCluster.spacing = 14 + + let controlRow = UIStackView(arrangedSubviews: [audioButton, playbackCluster, captionsButton]) controlRow.translatesAutoresizingMaskIntoConstraints = false controlRow.axis = .horizontal controlRow.alignment = .center - controlRow.distribution = .equalSpacing - controlRow.spacing = 14 + controlRow.distribution = .equalCentering + controlRow.spacing = 18 let stack = UIStackView(arrangedSubviews: [timeAndScrubRow, controlRow]) stack.translatesAutoresizingMaskIntoConstraints = false @@ -322,6 +340,9 @@ final class NativePlayerViewController: UIViewController { playPauseButton.heightAnchor.constraint(equalToConstant: 42), jumpForwardButton.widthAnchor.constraint(equalToConstant: 36), jumpForwardButton.heightAnchor.constraint(equalToConstant: 36), + audioButton.widthAnchor.constraint(equalToConstant: 36), + audioButton.heightAnchor.constraint(equalToConstant: 36), + playbackCluster.centerXAnchor.constraint(equalTo: controlRow.centerXAnchor), captionsButton.widthAnchor.constraint(equalToConstant: 36), captionsButton.heightAnchor.constraint(equalToConstant: 36) ]) @@ -430,6 +451,36 @@ final class NativePlayerViewController: UIViewController { return UIMenu(title: "Captions", children: trackActions + [delayActions]) } + private func audioMenu() -> UIMenu { + let selectedTrackID = backend.selectedAudioTrackID + let tracks = backend.audioTracks + let options = AudioOptionMapper.options(from: tracks) +#if DEBUG + print("[DreamioAudio] build-menu tracks=\(SubtitleDebugFormatter.trackSummary(tracks)) selected=\(selectedTrackID)") +#endif + let trackActions = options.map { track in + UIAction( + title: track.name, + state: track.id == selectedTrackID ? .on : .off + ) { [weak self] _ in + guard let self else { + return + } +#if DEBUG + print("[DreamioAudio] select-request id=\(track.id) name=\(track.name) before=\(self.backend.selectedAudioTrackID)") +#endif + self.backend.selectAudioTrack(id: track.id) +#if DEBUG + print("[DreamioAudio] select-result id=\(track.id) after=\(self.backend.selectedAudioTrackID) tracks=\(SubtitleDebugFormatter.trackSummary(self.backend.audioTracks))") +#endif + self.audioMenuSignature = nil + self.refreshControls() + } + } + + return UIMenu(title: "Audio", children: trackActions) + } + private func startProgressUpdates() { progressTimer?.invalidate() progressTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] _ in @@ -438,11 +489,13 @@ final class NativePlayerViewController: UIViewController { } private func refreshControls() { + let audioTracks = backend.audioTracks let subtitleTracks = backend.subtitleTracks playPauseButton.setImage(UIImage(systemName: backend.isPlaying ? "pause.fill" : "play.fill"), for: .normal) scrubber.isEnabled = backend.isSeekable jumpBackButton.isEnabled = backend.isSeekable jumpForwardButton.isEnabled = backend.isSeekable + updateAudioMenuIfNeeded(audioTracks: audioTracks) updateCaptionsMenuIfNeeded(subtitleTracks: subtitleTracks) elapsedLabel.text = PlaybackTimeFormatter.label(for: backend.currentTime) remainingLabel.text = "-\(PlaybackTimeFormatter.label(for: backend.remainingTime))" @@ -452,6 +505,26 @@ final class NativePlayerViewController: UIViewController { [scrubber, jumpBackButton, jumpForwardButton].forEach { $0.alpha = backend.isSeekable ? 1 : 0.45 } } + private func updateAudioMenuIfNeeded(audioTracks: [AudioTrack]) { + let selectedTrackID = backend.selectedAudioTrackID + let signature = trackMenuSignatureValue( + tracks: audioTracks, + selectedTrackID: selectedTrackID + ) + let hasSelectableTrack = AudioOptionMapper.options(from: audioTracks).count > 1 + audioButton.isEnabled = hasSelectableTrack + audioButton.alpha = hasSelectableTrack ? 1 : 0.45 + guard signature != audioMenuSignature else { + return + } + + audioMenuSignature = signature + audioButton.menu = audioMenu() +#if DEBUG + print("[DreamioAudio] refresh-menu enabled=\(audioButton.isEnabled) tracks=\(SubtitleDebugFormatter.trackSummary(audioTracks)) selected=\(selectedTrackID)") +#endif + } + private func updateCaptionsMenuIfNeeded(subtitleTracks: [SubtitleTrack]) { let selectedTrackID = backend.selectedSubtitleTrackID let signature = captionsMenuSignatureValue( @@ -476,11 +549,19 @@ final class NativePlayerViewController: UIViewController { tracks: [SubtitleTrack], selectedTrackID: Int32, delay: TimeInterval + ) -> String { + let trackSignature = trackMenuSignatureValue(tracks: tracks, selectedTrackID: selectedTrackID) + return "\(trackSignature)#delay=\(String(format: "%.1f", delay))" + } + + private func trackMenuSignatureValue( + tracks: [SubtitleTrack], + selectedTrackID: Int32 ) -> String { let trackSignature = tracks .map { "\($0.id):\($0.name)" } .joined(separator: "|") - return "\(trackSignature)#selected=\(selectedTrackID)#delay=\(String(format: "%.1f", delay))" + return "\(trackSignature)#selected=\(selectedTrackID)" } private func revealControls() { @@ -517,8 +598,10 @@ final class NativePlayerViewController: UIViewController { button.translatesAutoresizingMaskIntoConstraints = false button.setImage(UIImage(systemName: systemName), for: .normal) button.tintColor = .white - button.backgroundColor = UIColor.black.withAlphaComponent(0.35) + button.backgroundColor = UIColor.white.withAlphaComponent(0.12) button.layer.cornerRadius = 18 + button.layer.borderColor = UIColor.white.withAlphaComponent(0.16).cgColor + button.layer.borderWidth = 1 button.accessibilityLabel = label return button } diff --git a/Dreamio/StreamCandidate.swift b/Dreamio/StreamCandidate.swift index b2345a4..4902b5a 100644 --- a/Dreamio/StreamCandidate.swift +++ b/Dreamio/StreamCandidate.swift @@ -40,6 +40,8 @@ struct SubtitleTrack: Equatable { let name: String } +typealias AudioTrack = SubtitleTrack + #if DEBUG enum SubtitleDebugFormatter { static func candidateSummary(_ candidates: [SubtitleCandidate]) -> String { @@ -93,6 +95,12 @@ enum SubtitleOptionMapper { } } +enum AudioOptionMapper { + static func options(from tracks: [AudioTrack]) -> [AudioTrack] { + tracks.filter { $0.id >= 0 } + } +} + struct StreamClassification { let sourceKind: StreamSourceKind let containerGuess: StreamContainerGuess diff --git a/Dreamio/VLCNativePlaybackBackend.swift b/Dreamio/VLCNativePlaybackBackend.swift index 166d59b..6fcb029 100644 --- a/Dreamio/VLCNativePlaybackBackend.swift +++ b/Dreamio/VLCNativePlaybackBackend.swift @@ -18,6 +18,7 @@ final class VLCNativePlaybackBackend: NSObject, NativePlaybackBackend { var onFailure: ((Error) -> Void)? var onStateChange: (() -> Void)? var onSubtitleTracksChange: (() -> Void)? + var onAudioTracksChange: (() -> Void)? #if canImport(MobileVLCKit) private let mediaPlayer = VLCMediaPlayer() @@ -108,6 +109,19 @@ final class VLCNativePlaybackBackend: NSObject, NativePlaybackBackend { #endif } + func selectAudioTrack(id: Int32) { +#if canImport(MobileVLCKit) +#if DEBUG + logAudioTracks(reason: "before-select-\(id)") +#endif + mediaPlayer.currentAudioTrackIndex = id +#if DEBUG + logAudioTracks(reason: "after-select-\(id)") +#endif + onAudioTracksChange?() +#endif + } + func selectSubtitleTrack(id: Int32) { #if canImport(MobileVLCKit) didUserSelectSubtitleTrack = true @@ -197,6 +211,26 @@ final class VLCNativePlaybackBackend: NSObject, NativePlaybackBackend { #endif } + var audioTracks: [AudioTrack] { +#if canImport(MobileVLCKit) + let names = mediaPlayer.audioTrackNames as? [String] ?? [] + let indexes = mediaPlayer.audioTrackIndexes as? [NSNumber] ?? [] + return zip(indexes, names).map { index, name in + AudioTrack(id: index.int32Value, name: name) + } +#else + [] +#endif + } + + var selectedAudioTrackID: Int32 { +#if canImport(MobileVLCKit) + mediaPlayer.currentAudioTrackIndex +#else + -1 +#endif + } + var subtitleTracks: [SubtitleTrack] { #if canImport(MobileVLCKit) let names = mediaPlayer.videoSubTitlesNames as? [String] ?? [] @@ -269,6 +303,12 @@ final class VLCNativePlaybackBackend: NSObject, NativePlaybackBackend { } #if DEBUG + private func logAudioTracks(reason: String) { + let names = mediaPlayer.audioTrackNames as? [String] ?? [] + let indexes = mediaPlayer.audioTrackIndexes as? [NSNumber] ?? [] + print("[DreamioVLC] audio tracks reason=\(reason) names=\(names) indexes=\(indexes.map { $0.int32Value }) selected=\(mediaPlayer.currentAudioTrackIndex)") + } + private func logSubtitleTracks(reason: String) { let names = mediaPlayer.videoSubTitlesNames as? [String] ?? [] let indexes = mediaPlayer.videoSubTitlesIndexes as? [NSNumber] ?? [] @@ -360,6 +400,7 @@ extension VLCNativePlaybackBackend: VLCMediaPlayerDelegate { reapplyAutoSelectedSubtitleTrackIfNeeded(reason: stateName(mediaPlayer.state)) onReady?() onStateChange?() + onAudioTracksChange?() case .error: onFailure?(NativePlaybackError.playbackFailed) case .paused, .stopped, .ended: @@ -367,8 +408,10 @@ extension VLCNativePlaybackBackend: VLCMediaPlayerDelegate { case .esAdded: selectPreferredSubtitleTrackIfNeeded(reason: "esAdded") #if DEBUG + logAudioTracks(reason: "esAdded") logSubtitleTracks(reason: "esAdded") #endif + onAudioTracksChange?() onSubtitleTracksChange?() default: break diff --git a/docs/turns/2026-05-25-native-player-audio-tracks.html b/docs/turns/2026-05-25-native-player-audio-tracks.html new file mode 100644 index 0000000..2fbb9bc --- /dev/null +++ b/docs/turns/2026-05-25-native-player-audio-tracks.html @@ -0,0 +1,421 @@ + + + + + + Native Player Audio Track Selection + + + +
+
+
Dreamio turn document ยท May 25, 2026
+

Native player audio track selection

+

Dreamio's VLC-backed native player now exposes embedded audio tracks, adds a far-left audio menu to the control bar, and refreshes the player chrome with a more iOS-native glass treatment.

+
+ +
+

Summary

+

Added audio track discovery and selection to native playback so multi-language MKV and similar files can be switched without leaving the player.

+
+ +
+

Changes Made

+
    +
  • Extended NativePlaybackBackend with audio track state, a selection API, and an audio-track-change callback.
  • +
  • Read MobileVLCKit audio track names, indexes, and current selection from VLCMediaPlayer.
  • +
  • Added an audio menu button on the far left side of the native controls using the waveform.circle symbol.
  • +
  • Grouped skip/play/skip into a centered playback cluster so the play button stays visually centered.
  • +
  • Updated the control surface and buttons with translucent material, softer radius, subtle borders, and lighter glass-like control wells.
  • +
+
+ +
+

Context

+

The player already exposed embedded subtitle tracks through MobileVLCKit. The same streams often include multiple audio tracks, such as alternate languages or commentary, but the native player had no way to inspect or switch them.

+

The user-provided diagnostics showed VLC discovering embedded subtitle tracks while the UI still lacked track filtering for audio. This change follows the existing subtitle menu pattern instead of creating a separate player path.

+
+ +
+

Important Implementation Details

+
    +
  • AudioTrack currently aliases the existing SubtitleTrack value shape because both VLC APIs expose an integer id and display name.
  • +
  • The audio button is enabled only when VLC reports more than one selectable audio track, keeping single-track files quiet.
  • +
  • The backend fires onAudioTracksChange when VLC enters playback/buffering and when elementary streams are added.
  • +
  • Selection is applied with mediaPlayer.currentAudioTrackIndex, matching the existing subtitle selection style.
  • +
+
+ +
+

Relevant Diff Snippets

+

Rendered with @pierre/diffs/ssr from the current git diff.

+
+

Dreamio/NativePlaybackBackend.swift

Dreamio/NativePlaybackBackend.swift
+4
5 unmodified lines
6
7
8
9
10
11
12
13
14
15
16
17
5 unmodified lines
23
24
25
26
27
28
5 unmodified lines
var onFailure: ((Error) -> Void)? { get set }
var onStateChange: (() -> Void)? { get set }
var onSubtitleTracksChange: (() -> Void)? { get set }
var isPlaying: Bool { get }
var isSeekable: Bool { get }
var duration: TimeInterval { get }
var currentTime: TimeInterval { get }
var remainingTime: TimeInterval { get }
var position: Float { get }
var subtitleTracks: [SubtitleTrack] { get }
var selectedSubtitleTrackID: Int32 { get }
var subtitleDelay: TimeInterval { get }
5 unmodified lines
func togglePlayPause()
func seek(to position: Float)
func jump(by seconds: TimeInterval)
func selectSubtitleTrack(id: Int32)
func adjustSubtitleDelay(by seconds: TimeInterval)
@discardableResult
5 unmodified lines
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
5 unmodified lines
26
27
28
29
30
31
32
5 unmodified lines
var onFailure: ((Error) -> Void)? { get set }
var onStateChange: (() -> Void)? { get set }
var onSubtitleTracksChange: (() -> Void)? { get set }
var onAudioTracksChange: (() -> Void)? { get set }
var isPlaying: Bool { get }
var isSeekable: Bool { get }
var duration: TimeInterval { get }
var currentTime: TimeInterval { get }
var remainingTime: TimeInterval { get }
var position: Float { get }
var audioTracks: [AudioTrack] { get }
var selectedAudioTrackID: Int32 { get }
var subtitleTracks: [SubtitleTrack] { get }
var selectedSubtitleTrackID: Int32 { get }
var subtitleDelay: TimeInterval { get }
5 unmodified lines
func togglePlayPause()
func seek(to position: Float)
func jump(by seconds: TimeInterval)
func selectAudioTrack(id: Int32)
func selectSubtitleTrack(id: Int32)
func adjustSubtitleDelay(by seconds: TimeInterval)
@discardableResult
+

Dreamio/StreamCandidate.swift

Dreamio/StreamCandidate.swift
+8
39 unmodified lines
40
41
42
43
44
45
47 unmodified lines
93
94
95
96
97
98
39 unmodified lines
let name: String
}
+
#if DEBUG
enum SubtitleDebugFormatter {
static func candidateSummary(_ candidates: [SubtitleCandidate]) -> String {
47 unmodified lines
}
}
+
struct StreamClassification {
let sourceKind: StreamSourceKind
let containerGuess: StreamContainerGuess
39 unmodified lines
40
41
42
43
44
45
46
47
47 unmodified lines
95
96
97
98
99
100
101
102
103
104
105
106
39 unmodified lines
let name: String
}
+
typealias AudioTrack = SubtitleTrack
+
#if DEBUG
enum SubtitleDebugFormatter {
static func candidateSummary(_ candidates: [SubtitleCandidate]) -> String {
47 unmodified lines
}
}
+
enum AudioOptionMapper {
static func options(from tracks: [AudioTrack]) -> [AudioTrack] {
tracks.filter { $0.id >= 0 }
}
}
+
struct StreamClassification {
let sourceKind: StreamSourceKind
let containerGuess: StreamContainerGuess
+

Dreamio/VLCNativePlaybackBackend.swift

Dreamio/VLCNativePlaybackBackend.swift
+43
17 unmodified lines
18
19
20
21
22
23
80 unmodified lines
104
105
106
107
108
109
83 unmodified lines
193
194
195
196
197
198
58 unmodified lines
257
258
259
260
261
262
52 unmodified lines
315
316
317
318
319
320
1 unmodified line
322
323
324
325
326
327
328
329
17 unmodified lines
var onFailure: ((Error) -> Void)?
var onStateChange: (() -> Void)?
var onSubtitleTracksChange: (() -> Void)?
+
#if canImport(MobileVLCKit)
private let mediaPlayer = VLCMediaPlayer()
80 unmodified lines
#endif
}
+
func selectSubtitleTrack(id: Int32) {
#if canImport(MobileVLCKit)
didUserSelectSubtitleTrack = true
83 unmodified lines
#endif
}
+
var subtitleTracks: [SubtitleTrack] {
#if canImport(MobileVLCKit)
let names = mediaPlayer.videoSubTitlesNames as? [String] ?? []
58 unmodified lines
}
+
#if DEBUG
private func logSubtitleTracks(reason: String) {
let names = mediaPlayer.videoSubTitlesNames as? [String] ?? []
let indexes = mediaPlayer.videoSubTitlesIndexes as? [NSNumber] ?? []
52 unmodified lines
reapplyAutoSelectedSubtitleTrackIfNeeded(reason: stateName(mediaPlayer.state))
onReady?()
onStateChange?()
case .error:
onFailure?(NativePlaybackError.playbackFailed)
case .paused, .stopped, .ended:
1 unmodified line
case .esAdded:
selectInitialSubtitleTrackIfNeeded(reason: "esAdded")
#if DEBUG
logSubtitleTracks(reason: "esAdded")
#endif
onSubtitleTracksChange?()
default:
break
17 unmodified lines
18
19
20
21
22
23
24
80 unmodified lines
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
83 unmodified lines
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
58 unmodified lines
291
292
293
294
295
296
297
298
299
300
301
302
52 unmodified lines
355
356
357
358
359
360
361
1 unmodified line
363
364
365
366
367
368
369
370
371
372
17 unmodified lines
var onFailure: ((Error) -> Void)?
var onStateChange: (() -> Void)?
var onSubtitleTracksChange: (() -> Void)?
var onAudioTracksChange: (() -> Void)?
+
#if canImport(MobileVLCKit)
private let mediaPlayer = VLCMediaPlayer()
80 unmodified lines
#endif
}
+
func selectAudioTrack(id: Int32) {
#if canImport(MobileVLCKit)
#if DEBUG
logAudioTracks(reason: "before-select-\(id)")
#endif
mediaPlayer.currentAudioTrackIndex = id
#if DEBUG
logAudioTracks(reason: "after-select-\(id)")
#endif
onAudioTracksChange?()
#endif
}
+
func selectSubtitleTrack(id: Int32) {
#if canImport(MobileVLCKit)
didUserSelectSubtitleTrack = true
83 unmodified lines
#endif
}
+
var audioTracks: [AudioTrack] {
#if canImport(MobileVLCKit)
let names = mediaPlayer.audioTrackNames as? [String] ?? []
let indexes = mediaPlayer.audioTrackIndexes as? [NSNumber] ?? []
return zip(indexes, names).map { index, name in
AudioTrack(id: index.int32Value, name: name)
}
#else
[]
#endif
}
+
var selectedAudioTrackID: Int32 {
#if canImport(MobileVLCKit)
mediaPlayer.currentAudioTrackIndex
#else
-1
#endif
}
+
var subtitleTracks: [SubtitleTrack] {
#if canImport(MobileVLCKit)
let names = mediaPlayer.videoSubTitlesNames as? [String] ?? []
58 unmodified lines
}
+
#if DEBUG
private func logAudioTracks(reason: String) {
let names = mediaPlayer.audioTrackNames as? [String] ?? []
let indexes = mediaPlayer.audioTrackIndexes as? [NSNumber] ?? []
print("[DreamioVLC] audio tracks reason=\(reason) names=\(names) indexes=\(indexes.map { $0.int32Value }) selected=\(mediaPlayer.currentAudioTrackIndex)")
}
+
private func logSubtitleTracks(reason: String) {
let names = mediaPlayer.videoSubTitlesNames as? [String] ?? []
let indexes = mediaPlayer.videoSubTitlesIndexes as? [NSNumber] ?? []
52 unmodified lines
reapplyAutoSelectedSubtitleTrackIfNeeded(reason: stateName(mediaPlayer.state))
onReady?()
onStateChange?()
onAudioTracksChange?()
case .error:
onFailure?(NativePlaybackError.playbackFailed)
case .paused, .stopped, .ended:
1 unmodified line
case .esAdded:
selectInitialSubtitleTrackIfNeeded(reason: "esAdded")
#if DEBUG
logAudioTracks(reason: "esAdded")
logSubtitleTracks(reason: "esAdded")
#endif
onAudioTracksChange?()
onSubtitleTracksChange?()
default:
break
+

Dreamio/NativePlayerViewController.swift

Dreamio/NativePlayerViewController.swift
-7+90
8 unmodified lines
9
10
11
12
13
14
19 unmodified lines
34
35
36
37
38
39
40
41
7 unmodified lines
49
50
51
52
53
54
174 unmodified lines
229
230
231
232
233
234
15 unmodified lines
250
251
252
253
254
255
256
257
8 unmodified lines
266
267
268
269
270
271
272
273
274
275
276
277
44 unmodified lines
322
323
324
325
326
327
102 unmodified lines
430
431
432
433
434
435
2 unmodified lines
438
439
440
441
442
443
444
445
446
447
448
3 unmodified lines
452
453
454
455
456
457
18 unmodified lines
476
477
478
479
480
481
482
483
484
485
486
30 unmodified lines
517
518
519
520
521
522
523
524
8 unmodified lines
private var progressTimer: Timer?
private var isScrubbing = false
private var attachedSubtitleURLs: Set<URL>
private var captionsMenuSignature: String?
var onDismiss: (() -> Void)?
+
19 unmodified lines
private let controlsContainer: UIVisualEffectView = {
let view = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterialDark))
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 16
view.clipsToBounds = true
return view
}()
+
7 unmodified lines
private let playPauseButton = NativePlayerViewController.iconButton(systemName: "pause.fill", label: "Play or Pause")
private let jumpBackButton = NativePlayerViewController.iconButton(systemName: "gobackward.15", label: "Jump Back 15 Seconds")
private let jumpForwardButton = NativePlayerViewController.iconButton(systemName: "goforward.15", label: "Jump Forward 15 Seconds")
private let captionsButton = NativePlayerViewController.iconButton(systemName: "captions.bubble", label: "Captions")
+
private let elapsedLabel: UILabel = {
174 unmodified lines
self?.refreshControls()
}
}
}
+
private func startStartupTimer() {
15 unmodified lines
playPauseButton.addTarget(self, action: #selector(togglePlayPause), for: .touchUpInside)
jumpBackButton.addTarget(self, action: #selector(jumpBack), for: .touchUpInside)
jumpForwardButton.addTarget(self, action: #selector(jumpForward), for: .touchUpInside)
captionsButton.showsMenuAsPrimaryAction = true
playPauseButton.layer.cornerRadius = 21
scrubber.addTarget(self, action: #selector(scrubbingStarted), for: .touchDown)
scrubber.addTarget(self, action: #selector(scrubberChanged), for: .valueChanged)
scrubber.addTarget(self, action: #selector(scrubbingEnded), for: [.touchUpInside, .touchUpOutside, .touchCancel])
8 unmodified lines
timeAndScrubRow.alignment = .center
timeAndScrubRow.spacing = 8
+
let controlRow = UIStackView(arrangedSubviews: [jumpBackButton, playPauseButton, jumpForwardButton, captionsButton])
controlRow.translatesAutoresizingMaskIntoConstraints = false
controlRow.axis = .horizontal
controlRow.alignment = .center
controlRow.distribution = .equalSpacing
controlRow.spacing = 14
+
let stack = UIStackView(arrangedSubviews: [timeAndScrubRow, controlRow])
stack.translatesAutoresizingMaskIntoConstraints = false
44 unmodified lines
playPauseButton.heightAnchor.constraint(equalToConstant: 42),
jumpForwardButton.widthAnchor.constraint(equalToConstant: 36),
jumpForwardButton.heightAnchor.constraint(equalToConstant: 36),
captionsButton.widthAnchor.constraint(equalToConstant: 36),
captionsButton.heightAnchor.constraint(equalToConstant: 36)
])
102 unmodified lines
return UIMenu(title: "Captions", children: trackActions + [delayActions])
}
+
private func startProgressUpdates() {
progressTimer?.invalidate()
progressTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] _ in
2 unmodified lines
}
+
private func refreshControls() {
let subtitleTracks = backend.subtitleTracks
playPauseButton.setImage(UIImage(systemName: backend.isPlaying ? "pause.fill" : "play.fill"), for: .normal)
scrubber.isEnabled = backend.isSeekable
jumpBackButton.isEnabled = backend.isSeekable
jumpForwardButton.isEnabled = backend.isSeekable
updateCaptionsMenuIfNeeded(subtitleTracks: subtitleTracks)
elapsedLabel.text = PlaybackTimeFormatter.label(for: backend.currentTime)
remainingLabel.text = "-\(PlaybackTimeFormatter.label(for: backend.remainingTime))"
3 unmodified lines
[scrubber, jumpBackButton, jumpForwardButton].forEach { $0.alpha = backend.isSeekable ? 1 : 0.45 }
}
+
private func updateCaptionsMenuIfNeeded(subtitleTracks: [SubtitleTrack]) {
let selectedTrackID = backend.selectedSubtitleTrackID
let signature = captionsMenuSignatureValue(
18 unmodified lines
tracks: [SubtitleTrack],
selectedTrackID: Int32,
delay: TimeInterval
) -> String {
let trackSignature = tracks
.map { "\($0.id):\($0.name)" }
.joined(separator: "|")
return "\(trackSignature)#selected=\(selectedTrackID)#delay=\(String(format: "%.1f", delay))"
}
+
private func revealControls() {
30 unmodified lines
button.translatesAutoresizingMaskIntoConstraints = false
button.setImage(UIImage(systemName: systemName), for: .normal)
button.tintColor = .white
button.backgroundColor = UIColor.black.withAlphaComponent(0.35)
button.layer.cornerRadius = 18
button.accessibilityLabel = label
return button
}
8 unmodified lines
9
10
11
12
13
14
15
19 unmodified lines
35
36
37
38
39
40
41
42
43
44
45
7 unmodified lines
53
54
55
56
57
58
59
174 unmodified lines
234
235
236
237
238
239
240
241
242
243
244
15 unmodified lines
260
261
262
263
264
265
266
267
268
8 unmodified lines
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
44 unmodified lines
340
341
342
343
344
345
346
347
348
102 unmodified lines
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
2 unmodified lines
489
490
491
492
493
494
495
496
497
498
499
500
501
3 unmodified lines
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
18 unmodified lines
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
30 unmodified lines
598
599
600
601
602
603
604
605
606
607
8 unmodified lines
private var progressTimer: Timer?
private var isScrubbing = false
private var attachedSubtitleURLs: Set<URL>
private var audioMenuSignature: String?
private var captionsMenuSignature: String?
var onDismiss: (() -> Void)?
+
19 unmodified lines
private let controlsContainer: UIVisualEffectView = {
let view = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterialDark))
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 22
view.clipsToBounds = true
view.backgroundColor = UIColor.white.withAlphaComponent(0.08)
view.layer.borderColor = UIColor.white.withAlphaComponent(0.18).cgColor
view.layer.borderWidth = 1
return view
}()
+
7 unmodified lines
private let playPauseButton = NativePlayerViewController.iconButton(systemName: "pause.fill", label: "Play or Pause")
private let jumpBackButton = NativePlayerViewController.iconButton(systemName: "gobackward.15", label: "Jump Back 15 Seconds")
private let jumpForwardButton = NativePlayerViewController.iconButton(systemName: "goforward.15", label: "Jump Forward 15 Seconds")
private let audioButton = NativePlayerViewController.iconButton(systemName: "waveform.circle", label: "Audio Tracks")
private let captionsButton = NativePlayerViewController.iconButton(systemName: "captions.bubble", label: "Captions")
+
private let elapsedLabel: UILabel = {
174 unmodified lines
self?.refreshControls()
}
}
backend.onAudioTracksChange = { [weak self] in
DispatchQueue.main.async {
self?.refreshControls()
}
}
}
+
private func startStartupTimer() {
15 unmodified lines
playPauseButton.addTarget(self, action: #selector(togglePlayPause), for: .touchUpInside)
jumpBackButton.addTarget(self, action: #selector(jumpBack), for: .touchUpInside)
jumpForwardButton.addTarget(self, action: #selector(jumpForward), for: .touchUpInside)
audioButton.showsMenuAsPrimaryAction = true
captionsButton.showsMenuAsPrimaryAction = true
playPauseButton.layer.cornerRadius = 24
scrubber.addTarget(self, action: #selector(scrubbingStarted), for: .touchDown)
scrubber.addTarget(self, action: #selector(scrubberChanged), for: .valueChanged)
scrubber.addTarget(self, action: #selector(scrubbingEnded), for: [.touchUpInside, .touchUpOutside, .touchCancel])
8 unmodified lines
timeAndScrubRow.alignment = .center
timeAndScrubRow.spacing = 8
+
let playbackCluster = UIStackView(arrangedSubviews: [jumpBackButton, playPauseButton, jumpForwardButton])
playbackCluster.translatesAutoresizingMaskIntoConstraints = false
playbackCluster.axis = .horizontal
playbackCluster.alignment = .center
playbackCluster.distribution = .equalCentering
playbackCluster.spacing = 14
+
let controlRow = UIStackView(arrangedSubviews: [audioButton, playbackCluster, captionsButton])
controlRow.translatesAutoresizingMaskIntoConstraints = false
controlRow.axis = .horizontal
controlRow.alignment = .center
controlRow.distribution = .equalCentering
controlRow.spacing = 18
+
let stack = UIStackView(arrangedSubviews: [timeAndScrubRow, controlRow])
stack.translatesAutoresizingMaskIntoConstraints = false
44 unmodified lines
playPauseButton.heightAnchor.constraint(equalToConstant: 42),
jumpForwardButton.widthAnchor.constraint(equalToConstant: 36),
jumpForwardButton.heightAnchor.constraint(equalToConstant: 36),
audioButton.widthAnchor.constraint(equalToConstant: 36),
audioButton.heightAnchor.constraint(equalToConstant: 36),
playbackCluster.centerXAnchor.constraint(equalTo: controlRow.centerXAnchor),
captionsButton.widthAnchor.constraint(equalToConstant: 36),
captionsButton.heightAnchor.constraint(equalToConstant: 36)
])
102 unmodified lines
return UIMenu(title: "Captions", children: trackActions + [delayActions])
}
+
private func audioMenu() -> UIMenu {
let selectedTrackID = backend.selectedAudioTrackID
let tracks = backend.audioTracks
let options = AudioOptionMapper.options(from: tracks)
#if DEBUG
print("[DreamioAudio] build-menu tracks=\(SubtitleDebugFormatter.trackSummary(tracks)) selected=\(selectedTrackID)")
#endif
let trackActions = options.map { track in
UIAction(
title: track.name,
state: track.id == selectedTrackID ? .on : .off
) { [weak self] _ in
guard let self else {
return
}
#if DEBUG
print("[DreamioAudio] select-request id=\(track.id) name=\(track.name) before=\(self.backend.selectedAudioTrackID)")
#endif
self.backend.selectAudioTrack(id: track.id)
#if DEBUG
print("[DreamioAudio] select-result id=\(track.id) after=\(self.backend.selectedAudioTrackID) tracks=\(SubtitleDebugFormatter.trackSummary(self.backend.audioTracks))")
#endif
self.audioMenuSignature = nil
self.refreshControls()
}
}
+
return UIMenu(title: "Audio", children: trackActions)
}
+
private func startProgressUpdates() {
progressTimer?.invalidate()
progressTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] _ in
2 unmodified lines
}
+
private func refreshControls() {
let audioTracks = backend.audioTracks
let subtitleTracks = backend.subtitleTracks
playPauseButton.setImage(UIImage(systemName: backend.isPlaying ? "pause.fill" : "play.fill"), for: .normal)
scrubber.isEnabled = backend.isSeekable
jumpBackButton.isEnabled = backend.isSeekable
jumpForwardButton.isEnabled = backend.isSeekable
updateAudioMenuIfNeeded(audioTracks: audioTracks)
updateCaptionsMenuIfNeeded(subtitleTracks: subtitleTracks)
elapsedLabel.text = PlaybackTimeFormatter.label(for: backend.currentTime)
remainingLabel.text = "-\(PlaybackTimeFormatter.label(for: backend.remainingTime))"
3 unmodified lines
[scrubber, jumpBackButton, jumpForwardButton].forEach { $0.alpha = backend.isSeekable ? 1 : 0.45 }
}
+
private func updateAudioMenuIfNeeded(audioTracks: [AudioTrack]) {
let selectedTrackID = backend.selectedAudioTrackID
let signature = trackMenuSignatureValue(
tracks: audioTracks,
selectedTrackID: selectedTrackID
)
let hasSelectableTrack = AudioOptionMapper.options(from: audioTracks).count > 1
audioButton.isEnabled = hasSelectableTrack
audioButton.alpha = hasSelectableTrack ? 1 : 0.45
guard signature != audioMenuSignature else {
return
}
+
audioMenuSignature = signature
audioButton.menu = audioMenu()
#if DEBUG
print("[DreamioAudio] refresh-menu enabled=\(audioButton.isEnabled) tracks=\(SubtitleDebugFormatter.trackSummary(audioTracks)) selected=\(selectedTrackID)")
#endif
}
+
private func updateCaptionsMenuIfNeeded(subtitleTracks: [SubtitleTrack]) {
let selectedTrackID = backend.selectedSubtitleTrackID
let signature = captionsMenuSignatureValue(
18 unmodified lines
tracks: [SubtitleTrack],
selectedTrackID: Int32,
delay: TimeInterval
) -> String {
let trackSignature = trackMenuSignatureValue(tracks: tracks, selectedTrackID: selectedTrackID)
return "\(trackSignature)#delay=\(String(format: "%.1f", delay))"
}
+
private func trackMenuSignatureValue(
tracks: [SubtitleTrack],
selectedTrackID: Int32
) -> String {
let trackSignature = tracks
.map { "\($0.id):\($0.name)" }
.joined(separator: "|")
return "\(trackSignature)#selected=\(selectedTrackID)"
}
+
private func revealControls() {
30 unmodified lines
button.translatesAutoresizingMaskIntoConstraints = false
button.setImage(UIImage(systemName: systemName), for: .normal)
button.tintColor = .white
button.backgroundColor = UIColor.white.withAlphaComponent(0.12)
button.layer.cornerRadius = 18
button.layer.borderColor = UIColor.white.withAlphaComponent(0.16).cgColor
button.layer.borderWidth = 1
button.accessibilityLabel = label
return button
}
+ +
+

Expected Impact for End-Users

+

When a native-played file has multiple embedded audio tracks, users can open the new audio menu and choose the language or alternate mix they want. The play button remains centered, and the controls should feel more at home on iOS.

+
+ +
+

Validation

+
    +
  • Ran xcodebuild -workspace Dreamio.xcworkspace -scheme Dreamio -destination 'platform=iOS Simulator,name=iPhone 17' build: passed.
  • +
  • Attempted iPhone 16 simulator validation first, but that simulator was not installed on this machine.
  • +
  • No real-device playback stream was available in this turn, so actual multi-audio switching still needs a device check with an MKV that contains multiple audio languages.
  • +
+
+ +
+

Issues, Limitations, and Mitigations

+
    +
  • MobileVLCKit track discovery is event-driven, so the menu appears after VLC reports the stream tracks. The UI refreshes on playback, buffering, and elementary-stream-added events to mitigate delayed discovery.
  • +
  • The visual update uses UIKit blur/material styling rather than directly adopting iOS 26-only UIGlassEffect, keeping the project buildable for its current iOS 16 deployment target while following Liquid Glass principles.
  • +
  • Manual validation is still needed on a device with a known multi-audio stream.
  • +
+
+ +
+

Follow-up Work

+
    +
  • Use a known multi-language MKV on device and confirm the menu lists all audio tracks and switches without playback restart.
  • +
  • Consider parsing VLC track names into cleaner language labels if raw names are noisy.
  • +
  • Promote the track model from a typealias to distinct audio/subtitle structs if audio-specific metadata is added later.
  • +
+
+
+ + \ No newline at end of file