Skip to content

Instantly share code, notes, and snippets.

@bumaociyuan
Last active January 4, 2024 17:52
Show Gist options
  • Save bumaociyuan/711d3d2988bb38cd2b9638c0667a8dc2 to your computer and use it in GitHub Desktop.
Save bumaociyuan/711d3d2988bb38cd2b9638c0667a8dc2 to your computer and use it in GitHub Desktop.
ios 听筒和扬声器切换
let session = AVAudioSession.sharedInstance()
try? session.setActive(true)
if !isSpeakerMode {
// NSLog(@"Device is close to user");
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
try AVAudioSession.sharedInstance().overrideOutputAudioPort(.none)
} catch _ {}
} else {
// NSLog(@"Device is not close to user");
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
} catch _ {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment