2011-05-25 55 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]; 
    } 
}