CodexBloom - Programming Q&A Platform

iOS 17: how to to use multiple audio outputs with AVAudioEngine in Swift

πŸ‘€ Views: 33 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-11
iOS AVAudioEngine AVAudioSession Swift Audio

I'm testing a new approach and After trying multiple solutions online, I still can't figure this out. I'm sure I'm missing something obvious here, but I'm currently working with an scenario with using multiple audio outputs simultaneously in my iOS 17 app using AVAudioEngine... The goal is to output sound through both the device speaker and AirPlay at the same time. I set up my audio engine and tried to connect multiple outputs, but I'm only able to hear sound from one output at a time. I've already looked into using the `AVAudioSession` category settings and attempted both `.playAndRecord` and `.multiRoute` categories, but neither yielded the expected behavior. Here’s a simplified version of my code: ```swift import AVFoundation class AudioManager { var audioEngine: AVAudioEngine! var playerNode: AVAudioPlayerNode! init() { setupAudioEngine() } private func setupAudioEngine() { audioEngine = AVAudioEngine() playerNode = AVAudioPlayerNode() audioEngine.attach(playerNode) let mixer = audioEngine.mainMixerNode audioEngine.connect(playerNode, to: mixer, format: nil) } func playSound() { guard let url = Bundle.main.url(forResource: "audioFile", withExtension: "mp3") else { return } let audioFile = try! AVAudioFile(forReading: url) playerNode.scheduleFile(audioFile, at: nil, completionHandler: nil) playerNode.play() setAudioSession() } private func setAudioSession() { let session = AVAudioSession.sharedInstance() do { try session.setCategory(.playAndRecord, options: [.allowAirPlay, .defaultToSpeaker]) try session.setActive(true) } catch { print("behavior setting audio session: \(behavior)") } } } ``` After running this code, I get no sound from the AirPlay device when the `AVAudioSession` is set to play through the speaker. I also receive a warning in the console stating, "This app is attempting to use multiple audio routes, which may not be supported on the device." Has anyone successfully managed to output audio to multiple routes with AVAudioEngine? Any insights or suggestions would be greatly appreciated! How would you solve this? How would you solve this? Is this even possible? This is for a microservice running on Ubuntu 20.04.