AVAudioSession speaker preferences do not programmatically disconnect an active AirPlay route

GitHub source

Apple confirmed. This page is generated from GitHub Issue #29; GitHub remains the canonical authoring and discussion source.

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:

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 allowsExternalPlayback values.
  • 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#

  1. Do not use audio-session category churn or .defaultToSpeaker as an AirPlay-disconnect mechanism.
  2. Detect whether currentRoute.outputs contains a non-built-in/AirPlay output when the product wants playback returned locally.
  3. Present AVRoutePickerView and let the user select the desired output, as recommended by Apple DTS.
  4. Use allowsExternalPlayback to express the individual player's external-playback eligibility, not as a route teardown API.
  5. Keep route state and player state separately observable; after closing PiP or replacing a player, do not infer that the AirPlay route disappeared.
  6. 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#