2011-05-25 11 views

答えて

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]; 
    } 
} 
関連する問題