Source#
Apple Developer Forums — “Disconnect from AirPlay device programmatically” (Dec 2025, still current), answered by Apple DTS Engineer Greg:
https://developer.apple.com/forums/thread/810035
Apple documentation:
AVAudioSession: https://developer.apple.com/documentation/avfaudio/avaudiosessionAVAudioSession.Category: https://developer.apple.com/documentation/avfaudio/avaudiosession/category-swift.structAVAudioSession.PortOverride: https://developer.apple.com/documentation/avfaudio/avaudiosession/portoverrideAVRoutePickerView: https://developer.apple.com/documentation/avkit/avroutepickerviewAVPlayer.allowsExternalPlayback: https://developer.apple.com/documentation/avfoundation/avplayer/allowsexternalplayback
Problem#
An app has an active AirPlay route and wants to return playback to the local device when the user closes a playback/PiP experience. A tempting workaround is:
try session.setCategory(.playAndRecord, options: .defaultToSpeaker)
try session.setActive(true)or a speaker output override.
Apple DTS states that there is no reliable public API for programmatically disconnecting the app's audio playback from an active AirPlay route. defaultToSpeaker is not an AirPlay-disconnect command; it affects the built-in receiver-vs-speaker choice when no other route is connected. DTS recommends presenting AVRoutePickerView so the user can choose the route.
A related forum follow-up reports that AVPlayer.allowsExternalPlayback = false can still leave audio on the existing AirPlay route. That observation is useful but has not received an Apple-confirmed explanation in the thread.
Evidence level#
DOCUMENTED#
AVAudioSession expresses an app's audio behavior while the system manages routing. Apple documents playback categories as AirPlay-capable and exposes route inspection/selection UI through AVKit. AVPlayer.allowsExternalPlayback controls whether that player may switch to external playback mode; it is not documented as a command that tears down the system audio route.
APPLE ENGINEERING STATEMENT#
Apple DTS explicitly says there is no reliable way to programmatically disconnect an app's audio playback from AirPlay, aside from user route selection. DTS also explains that .defaultToSpeaker only has the expected effect when choosing the built-in speaker instead of the built-in receiver and should not displace an already-connected AirPlay route.
OBSERVED#
The reporter saw the category-change technique work inconsistently across OS versions. A follow-up reports audio continuing through AirPlay for a later player even when that player's allowsExternalPlayback is false.
IMPLEMENTATION EVIDENCE#
None is required to establish the supported control boundary: Apple DTS has stated the public behavior. Internal route arbitration remains system-owned.
INFERENCE#
An active AirPlay route and an individual player's external-video-playback eligibility are separate state dimensions. Consequently, changing a player-level property should not be assumed to mutate the global/session audio route.
UNKNOWN#
- Exact internal arbitration rules when multiple AVPlayers have different
allowsExternalPlaybackvalues. - Whether future SDKs will add a user-authorized programmatic disconnect API.
- Exact behavior differences for mirrored versus nonmirrored AirPlay and different audio-session categories on every OS release.
Investigation / Explanation#
Separate three layers:
AVPlayer external-playback eligibility
↓
AVAudioSession route policy/preferences
↓
system-selected active output route
↓
AirPlay receiver
Do not treat these operations as equivalent:
player.allowsExternalPlayback = false
session category/options change
session output-port preference
user changes route in AVRoutePickerView / system UI
The strongest runtime discriminator is AVAudioSession.sharedInstance().currentRoute.outputs before and after each operation. Also record player.isExternalPlaybackActive independently: a player can be outside AVPlayer external-video mode while the app/session audio route still targets AirPlay.
The forum's failed workaround is especially useful because it isolates intent from mechanism: .defaultToSpeaker expresses a local receiver/speaker preference under its documented conditions; it does not own the active AirPlay connection.
Verification matrix#
Use one device, one AirPlay receiver, and the same local media:
A. active AirPlay → set .defaultToSpeaker
B. active AirPlay → overrideOutputAudioPort(.speaker) where category permits
C. active AirPlay → player.allowsExternalPlayback = false
D. active AirPlay → user selects iPhone/iPad in AVRoutePickerView
E. no AirPlay → .playAndRecord + .defaultToSpeaker
Capture for every transition:
currentRoute.outputs portType/name
AVAudioSession category/options
routeChangeNotification reason
player.allowsExternalPlayback
player.isExternalPlaybackActive
player.rate/currentTime
audio location actually heard
Expected contract-level discriminator:
A leaves AirPlay active
→ consistent with DTS guidance; defaultToSpeaker is not disconnect
D changes currentRoute back to built-in output
→ supported user-owned route-selection path
C disables external player mode but currentRoute remains AirPlay
→ player external-playback eligibility and audio-route ownership are proven separate for that run
Solution / Guideline#
- Do not use audio-session category churn or
.defaultToSpeakeras an AirPlay-disconnect mechanism. - Detect whether
currentRoute.outputscontains a non-built-in/AirPlay output when the product wants playback returned locally. - Present
AVRoutePickerViewand let the user select the desired output, as recommended by Apple DTS. - Use
allowsExternalPlaybackto express the individual player's external-playback eligibility, not as a route teardown API. - Keep route state and player state separately observable; after closing PiP or replacing a player, do not infer that the AirPlay route disappeared.
- If the product requires forced programmatic disconnect, treat that as an unsupported capability/enhancement request rather than relying on OS-version-dependent routing side effects.
Reusable engineering rule#
A playback destination has multiple owners: player eligibility, audio-session policy, and the system's active route are different states.
Debug AirPlay transitions as:
player policy
→ session routing policy
→ currentRoute
→ user route selection
→ rendered destination
A setting that changes one layer is not evidence that another layer changed. Verify currentRoute directly before treating a player or audio-session preference as a routing command.
References#
- Apple Developer Forums — Apple DTS answer: https://developer.apple.com/forums/thread/810035
- Apple —
AVAudioSession: https://developer.apple.com/documentation/avfaudio/avaudiosession - Apple — audio-session categories / AirPlay support: https://developer.apple.com/documentation/avfaudio/avaudiosession/category-swift.struct
- Apple —
AVAudioSession.PortOverride: https://developer.apple.com/documentation/avfaudio/avaudiosession/portoverride - Apple —
AVRoutePickerView: https://developer.apple.com/documentation/avkit/avroutepickerview - Apple —
AVPlayer.allowsExternalPlayback: https://developer.apple.com/documentation/avfoundation/avplayer/allowsexternalplayback