Skip to content

Instantly share code, notes, and snippets.

@ydnar
Created June 13, 2019 23:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ydnar/912b88f19bc2d937f3b7ffe03354484f to your computer and use it in GitHub Desktop.
Save ydnar/912b88f19bc2d937f3b7ffe03354484f to your computer and use it in GitHub Desktop.
// Thanks https://amorten.com/blog/2017/ios-bluetooth-and-microphone-input/
func configure() {
// ...
let session = AVAudioSession.sharedInstance()
let sampleRate = 48000.0
do {
try session.setCategory(
.playAndRecord, // Simultaneously play and record audio
mode: .default, // .voiceChat somehow breaks with .allowBluetoothA2DP, making microphone not work
options: [
.mixWithOthers, // Mix audio with other apps
.allowBluetoothA2DP, // Allow the use of Bluetooth devices like AirPods (DO NOT USE .allowBluetooth, which forces audio to 16kHz
.defaultToSpeaker,
.allowAirPlay,
]
)
try session.setPreferredSampleRate(sampleRate)
print("Configured audio session: \(session.sampleRate)")
} catch {
print("Error configuring audio session: \(error)")
return
}
}
@amos-yau
Copy link

I've tried this but not working for my AirPods Pro. Playing audio route to AirPods is fine, but recording by AirPods is failed, still using iPhone builtin microphone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment