Support AirPods Route Changes During Audio Recording
Someone starts a voice note with AirPods, puts them in their case, and keeps talking. Your recorder now has a product decision to make: keep the existing recording safe, explain the input change, and decide whether to resume with another microphone.
The accessory name cannot answer that decision. An audio route is live state, and its input format can change. This is a useful September hardware-season check even when your recording code uses established APIs.
Read the input that is actually active
Observe AVAudioSession.routeChangeNotification. Apple documents that it is posted on a secondary thread. Move control work onto the executor that owns your recorder and read the current route there; do not send a mutable engine graph through arbitrary notification callbacks.
Apple’s route-change guidance calls out changes to sample rate, channel count, and other hardware values. Re-read them after the transition. A preferred sample rate is not evidence that the current stream uses it.
Here is a small snapshot for an input-node recorder. Call it from the recorder’s control path after session/engine configuration, not from the real-time audio render callback:
import AVFAudio
struct InputSignature: Equatable, Sendable {
var portID: String
var sampleRate: Double
var channels: UInt32
}
@MainActor
func inputSignature(
session: AVAudioSession,
engine: AVAudioEngine
) -> InputSignature? {
guard let port = session.currentRoute.inputs.first else { return nil }
let format = engine.inputNode.outputFormat(forBus: 0)
guard format.sampleRate.isFinite, format.sampleRate > 0,
format.channelCount > 0
else { return nil }
return InputSignature(
portID: port.uid,
sampleRate: format.sampleRate,
channels: format.channelCount
)
}
This signature intentionally covers a small recorder’s input identity, rate, and channel count. A multiroute or processing-heavy app should also track the format/layout and graph properties that its writer and converters depend on. Do not treat three fields as a universal AVAudioFormat compatibility test.
End a segment before changing its assumptions
A conservative voice-note policy keeps the current segment only if that input signature remains valid and unchanged:
enum RouteDecision: Equatable {
case waitForInput, continueSegment, finishSegment
}
func routeDecision(
active: InputSignature?,
current: InputSignature?
) -> RouteDecision {
guard let active else { return .waitForInput }
guard let current, current.sampleRate.isFinite,
current.sampleRate > 0, current.channels > 0
else {
return .finishSegment
}
return active == current ? .continueSegment : .finishSegment
}
active describes the segment that is already recording. When it is nil, this function never starts recording merely because an input appeared. Starting or resuming remains an explicit command.
A finish decision means the recorder stops accepting new buffers for that segment, drains already accepted work through its writer, closes the file, and records the transition in session metadata. Serialize that lifecycle so repeated route notifications cannot close the same writer twice or attach a new tap while an old one still owns the input.
This policy helper is not a complete recorder or an engine-restart recipe. In particular, an engine configuration change or interruption can require stopping/rebuilding even when this compact signature compares equal. Handle those events through the same owned lifecycle, with their own reason, instead of assuming route equality proves the engine is still running.
Make a microphone switch visible
For an interview or voice note, an unexpected switch from a headset microphone to the phone’s microphone is worth presenting to the person recording. Preserve the completed segment and offer Resume using the currently available input.
If your product intentionally resumes automatically, make that behavior explicit and show the new microphone. Create a new segment with a unique ID and actual format metadata. Never reopen the old filename in a way that truncates the recording already captured.
At export time, convert segments into the chosen output format and preserve any real gap between them. Concatenating bytes from files with different formats is not an audio conversion strategy. Avoid representing an interrupted interval as captured speech.
Test the policy and the device separately
The pure fixture checks an unchanged input, a different port with the same format, changed sample rate, changed channel count, no input, an invalid rate, and no active segment. Each transition has an explicit expected decision.
On device, record while disconnecting the headset, switching to a wired interface, and receiving an interruption. Inspect the resulting files, not just the recording indicator. Confirm old segments remain playable, resumed segments identify the correct input, and the UI never says Recording while no buffers arrive.
September 2026 verification: the policy fixture passed those runtime assertions. The AVAudioSession/AVAudioEngine snapshot adapter type-checked for iOS 17 with SDK 27.0. No physical AirPods route transition or uninterrupted-recording claim is made by these checks.