2011-05-25 25 views
0

使用我的iPad應用程序錄制聲音時,如何知道聲音的來源是來自內置麥克風還是耳機麥克風?如何確定錄音源

附加信息:iOS版本4.2及以上版本。

回答

0

確定此方法的方法是輪詢硬件並查詢當前的音頻路由。使用AudioSessionGetProperty對象獲取音頻路徑。

example by @TPoschel應該讓你在正確的軌道上。

- (void)playSound:(id) sender 
{ 
    if(player){ 

     CFStringRef route; 
     UInt32 propertySize = sizeof(CFStringRef); 
     AudioSessionInitialize(NULL, NULL, NULL, NULL); 
     AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route); 

     if((route == NULL) || (CFStringGetLength(route) == 0)){ 
      // Silent Mode 
      NSLog(@"AudioRoute: SILENT"); 
     } else { 
      NSString* routeStr = (NSString*)route; 
      NSLog(@"AudioRoute: %@", routeStr); 

      /* Known values of route: 
      * "Headset" 
      * "Headphone" 
      * "Speaker" 
      * "SpeakerAndMicrophone" 
      * "HeadphonesAndMicrophone" 
      * "HeadsetInOut" 
      * "ReceiverAndMicrophone" 
      * "Lineout" 
      */ 

      NSRange headphoneRange = [routeStr rangeOfString : @"Headphone"]; 
      NSRange headsetRange = [routeStr rangeOfString : @"Headset"]; 
      NSRange receiverRange = [routeStr rangeOfString : @"Receiver"]; 
      NSRange speakerRange = [routeStr rangeOfString : @"Speaker"]; 
      NSRange lineoutRange = [routeStr rangeOfString : @"Lineout"]; 

      if (headphoneRange.location != NSNotFound) { 
       // Don't change the route if the headphone is plugged in. 
      } else if(headsetRange.location != NSNotFound) { 
       // Don't change the route if the headset is plugged in. 
      } else if (receiverRange.location != NSNotFound) { 
       // Change to play on the speaker 
       UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
       AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride); 
      } else if (speakerRange.location != NSNotFound) { 
       // Don't change the route if the speaker is currently playing. 
      } else if (lineoutRange.location != NSNotFound) { 
       // Don't change the route if the lineout is plugged in. 
      } else { 
       NSLog(@"Unknown audio route."); 
      } 
     } 

     [player play]; 
    } 
}