Skip to content

Commit c2e4563

Browse files
committed
fix: Avoid overriding to default speaker
1 parent 820c55e commit c2e4563

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

Jarvis/Jarvis/SpeechRecognizer.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ class SpeechRecognizer: ObservableObject {
108108

109109
// Set up the audio session to allow playback
110110
let audioSession = AVAudioSession.sharedInstance()
111-
try audioSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker])
112-
try audioSession.overrideOutputAudioPort(.speaker) // 🔈 Force speaker output
111+
try audioSession.setCategory(.playback, mode: .default, options: .mixWithOthers)
113112
try audioSession.setActive(true)
114113

115114
for output in audioSession.currentRoute.outputs {
@@ -120,11 +119,22 @@ class SpeechRecognizer: ObservableObject {
120119
self.player = try AVAudioPlayer(contentsOf: audioFileURL)
121120
self.player?.volume = 1.0
122121
self.player?.prepareToPlay()
123-
let duration = player?.duration
124-
if duration == 0 {
125-
Log.debug("❌ Invalid audio file. Duration = 0")
126-
} else if let dur = duration {
127-
Log.debug("Audio duration: \(dur)")
122+
var msg: String
123+
if let duration = player?.duration {
124+
if duration == 0 {
125+
msg = "Invalid audio file. Duration = 0"
126+
Log.warn("\(msg)")
127+
} else {
128+
let formattedDuration = String(format: "%.2f", duration)
129+
msg = "Audio duration: \(formattedDuration)"
130+
Log.debug("\(msg)")
131+
}
132+
} else {
133+
msg = "Failed to retrieve audio duration"
134+
Log.warn("\(msg)")
135+
}
136+
DispatchQueue.main.async {
137+
self.recognizedText = msg
128138
}
129139
self.player?.play()
130140

0 commit comments

Comments
 (0)