Last active
January 4, 2024 17:52
-
-
Save bumaociyuan/711d3d2988bb38cd2b9638c0667a8dc2 to your computer and use it in GitHub Desktop.
ios 听筒和扬声器切换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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