Hello there, I'm trying to implement feature which uses AirPlay with Apple TV. I want to disconnect from the device programmatically when something happens. Under something I mean a situation when a user wants to stop broadcasting (for example close the PiP window on his phone). I use this snippet:
try audioSession.setCategory(.playAndRecord, options: .defaultToSpeaker)
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
It works fine sometimes but not always (it works on iOS 18 but it doesn't on iOS 17 or ). So I thought it's a bug and create a ticker to feedback assistant (FB21220013). The support told me write a post on the forum.
Hello @erawq,
There is no reliable way to disconnect your app's audio playback from an AirPlay route (aside from the user taking action to change the route in Control Center for example).
From AVAudioSessionTypes.h:
"-
AVAudioSessionCategoryPlayAndRecord: /// DefaultToSpeaker will default to false, but >can be set to true, routing to Speaker /// (instead of Receiver) when no other audio route is connected."
The key phrasing here is "(instead of Receiver) when no other audio route is connected."
In other words:
try audioSession.setCategory(.playAndRecord, options: .defaultToSpeaker)
This call is only expected to have an effect when audio is currently being routed to the device's receiver (the speaker which on most devices is at the top near the front-facing camera). In your case, audio is being routed to an AirPlay route, so it is expected that this call should not have an effect.
If you would like the capability of programmatically disconnecting from an AirPlay route, I suggest that you open a new Feedback to file that enhancement request.
Otherwise, my recommendation is that you do the following:
Under something I mean a situation when a user wants to stop broadcasting (for example close the PiP window on his phone).
When the user takes that action, if you detect a non-builtIn output for the currentRoute, you can present an AVRoutePickerView to the user, and then they can specify where they would like to route the audio to.
--Greg