iOS 헤드폰 작업

10439 단어 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];  });  }  }]; } 

아이폰 감지 이어폰 삽입/뽑기
핸드폰의 현재 사용 여부를 판단하는 것은 내장된 마이크이다(이 방법으로 삽입된 이어폰에 마이크가 있는지 판단할 수 있다)
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; } 

좋은 웹페이지 즐겨찾기