Skip to content

Instantly share code, notes, and snippets.

@bumaociyuan

bumaociyuan/hehe.swift

Last active Apr 27, 2017
Embed
What would you like to do?
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