Audio Session Troubleshooting
Real-Time Pipeline Is Silentโ
Symptoms: engine.start() succeeds but no audio comes through the tap.
Checklist:
-
Microphone permission โ confirm
AVAudioSession.recordPermission == .granted -
Audio session activated โ call
AVAudioSession.sharedInstance().setActive(true)beforeengine.start() -
Correct format in installTap โ the format passed to
installTapmust match theAVAudioInputNode's output format:let inputFormat = engine.inputNode.outputFormat(forBus: 0)
// If inputFormat.sampleRate != 48000, use AVAudioMixerNode to resample -
installTap before start โ
installTapmust be called beforeengine.start()
AVAudioSession Activation Failedโ
let session = AVAudioSession.sharedInstance()
try session.setCategory(.playAndRecord, options: [.mixWithOthers, .allowBluetooth])
try session.setActive(false, options: .notifyOthersOnDeactivation)
try session.setActive(true)
Engine Stops After Phone Callโ
NotificationCenter.default.addObserver(
forName: AVAudioSession.interruptionNotification,
object: nil, queue: .main
) { [weak engine] notification in
guard let info = notification.userInfo,
let t = info[AVAudioSessionInterruptionTypeKey] as? UInt,
let type = AVAudioSession.InterruptionType(rawValue: t) else { return }
switch type {
case .ended:
try? engine?.start()
default: break
}
}
Sample Rate Mismatch Warningโ
Always query the actual input format:
let hwFormat = engine.inputNode.outputFormat(forBus: 0)
guard let targetFormat = AVAudioFormat(
commonFormat: .pcmFormatFloat32, sampleRate: 48_000, channels: 1, interleaved: false
) else { return }
mixer.installTap(onBus: 0, bufferSize: 480, format: targetFormat) { buffer, _ in
// buffer is now guaranteed 48kHz Float32 mono
}