iOS耳机操作
iOS在7之后增加的麥克風權限的申請,代碼如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 | AVAudioSession *avSession = [AVAudioSession sharedInstance]; if ([avSession respondsToSelector:@selector(requestRecordPermission:)]) { [avSession requestRecordPermission:^(BOOL available) { if (available) { // 有麥克風權限 } else { dispatch_async(dispatch_get_main_queue(), ^{ [[[UIAlertView alloc] initWithTitle:@"無法錄音" message:@"請在“設置-隱私-麥克風”選項中允許xx訪問你的麥克風" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil] show]; }); } }]; } |
iphone檢測耳機插入/拔出
判斷手機當前是否使用的是內置的麥克風(可以用此方法判斷插入的耳機是否有麥克風)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | - (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, kAudioSession_AudioRouteKey_Inputs)) { 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 isEqualToString:(__bridge NSString *)kAudioSessionInputRoute_BuiltInMic]) { return YES; } } } } else { // 耳機沒有mic return YES; } return NO; } |
轉載于:https://www.cnblogs.com/lovewx/p/4155782.html
總結
- 上一篇: OC学习之面向对象三大特征
- 下一篇: XMPP协议简单介绍