3

Right now, when the user presses the AVRoutePickerView, they get a choice of iPhone and other AirPlay targets. And picking iPhone uses the speakerphone to play back audio:

Current Situation

I'd like there to be an option to switch between iPhone and Speaker, where iPhone routes the audio to the internal handset speaker, and Speaker routes audio to the speakerphone. That's how other apps on the phone seem to work (Phone, Google Voice, and some others):

The way it should be

user496854
  • 6,461
  • 10
  • 47
  • 84
  • Just a guess, try adding/removing `.defaultToSpeaker` option in `AVAudioSession` – Dmytro Rostopira Dec 25 '20 at 10:03
  • Thanks, but I already tried that. I tried multiple AVAudioSession modes, categories, and options. Maybe there's a specific combination that's required for this to work? – user496854 Dec 25 '20 at 10:16

1 Answers1

3

I figured it out, but I'm pretty pissed off at Apple for basically not having any documentation about how things are supposed to work!

In essence, you have to overrideOutputAudioPort on AVAudioSession after calling setCategory, but before calling setActive. It doesn't matter which route you want to use for the override (.speaker or .none) -- either way, it'll now allow you to chose either the Speaker or iPhone in AVRoutePickerView. Here's an example:

try AVAudioSession.sharedInstance().setCategory(.playAndRecord)
try AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
try AVAudioSession.sharedInstance().setActive(true)
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
user496854
  • 6,461
  • 10
  • 47
  • 84
  • Note that presense of the speaker option in AVRoutePickerView depends on the mode you've set. For example, if you use AVAudioSession.Mode.videoChat, speaker won't show up but is automatically used unless you lift receiver to your ear. Per Apple's header about some of the modes: "Reduces the number of allowable audio routes to be only those that are appropriate for video chat applications." BTW, if you option-click in Xcode on each mode in Apple's AVAudioSession header, you'll get more details than in Apple's docs on the web or even better than the comments in the header itself. – Smartcat Feb 04 '21 at 20:46