IOS headset operation, ios headset
The code for applying for microphone permissions added after iOS 7 is as follows:
123456789101112 |
AVAudioSession * avSession = [AVAudioSession sharedInstance]; if ([avSession respondsToSelector: @ selector (listener :)]) {[avSession requestRecordPermission: ^ (BOOL available) {if (available) {// microphone permission} else {dispatch_async (dispatch_get_main_queue (), ^ {[[[UIAlertView alloc] initWithTitle: @ "unable to record" message: @ "Please allow xx to access your microphone in the" Settings-privacy-microphone "option" delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil] show] ;}}}] ;}
|
Iphone detection headset insertion/removal
Determine whether the phone is currently using a built-in microphone (you can use this method to determine whether the inserted headset has a microphone)
123456789101112131415161718192021222324252627282930313233 |
-(BOOL) isCurrentUsingBuildInMicrophone {NSError * error = nil; BOOL result = YES; result = [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: & error]; if (! Result) {NSLog (@ "% @", error); return YES;} result = [[AVAudioSession sharedInstance] setActive: YES error: & error]; if (! Result) {NSLog (@ "setActive failed"); return YES;} CFDictionaryRef ards; UInt32 size = sizeof (CFDictionaryRef); OSStatus OS = AudioSessionGetProperty (kAudioSessionProperty_AudioRouteDescription, & size, & ards); if (OS = kAudioSessionNoError & ards & CFDictionaryGetValue (ards, Rules) {NSArray * inputs = (_ bridge NSArray *) CFDictionaryGetValue (ards, kAudioSession_AudioRouteKey_Inputs); if (inputs & inputs. count> 0) {for (NSDictionary * dic in inputs) {NSString * type = dic [(_ bridge NSString *) kAudioSession_AudioRouteKey_Type]; if ([type is0000tostring :( _ bridge NSString *) kAudioSessionInputRoute_BuiltInMic]) {return YES ;}}} else {// NO mic return YES For the headset;} return NO ;}
|