检测耳机是否已插入iOS设备

时间:2012-05-21 12:30:53

标签: ios xcode headphones

ios设备上有一个互动电影。当电影开始(点击)时,视频开头的人会问你插上耳机,如果插上,那么视频应该会自动直接跳到故事中(直接转到视频故事)。我该怎么办?以及如何编写代码?

3 个答案:

答案 0 :(得分:3)

首先,您必须注册AudioRoute Changes: -

AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange,
                                     audioRouteChangeListenerCallback,
                                 self);

此处您可以描述更改路线的原因: -

CFDictionaryRef routeChangeDictionary = inPropertyValue;

  CFNumberRef routeChangeReasonRef =
  CFDictionaryGetValue (routeChangeDictionary,
                        CFSTR (kAudioSession_AudioRouteChangeKey_Reason));

  SInt32 routeChangeReason;

      CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);

  if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) 
  {
       // your statements for headset unplugged

  }
  if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
  {
       // your statements for headset plugged                             
  }

答案 1 :(得分:0)

这可能是另一种方式:

CFStringRef newRoute;
size = sizeof(CFStringRef);
XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute), "couldn't get new audio route");
if (newRoute)
{
    CFShow(newRoute);
    if (CFStringCompare(newRoute, CFSTR("HeadsetInOut"), NULL) == kCFCompareEqualTo) // headset plugged in
          {
...
          }
    else if (CFStringCompare(newRoute, CFSTR("SpeakerAndMicrophone"), NULL) == kCFCompareEqualTo){
     ....
     }
}

答案 2 :(得分:0)

首先检查设备是否已连接到任何耳机。

+(BOOL)isHeadsetPluggedIn {

AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
for (AVAudioSessionPortDescription* desc in [route outputs]) {
    if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
        return YES;
}
return NO;
}

然后根据bool值,编写自己的条件。像下面的东西..

if (isHeadphonesConnected) {
    //Write your own code here
}else{

}

此外,如果您在屏幕中移除了耳机,也可以注册一个通知。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(audioRoutingListenerCallback:)
                                        name:AVAudioSessionRouteChangeNotification
                                           object:nil];

- (void)audioRoutingListenerCallback:(NSNotification*)notification
{
    NSDictionary *interuptionDict = notification.userInfo;

    NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];

    switch (routeChangeReason) {

        case AVAudioSessionRouteChangeReasonNewDeviceAvailable:

            NSLog(@"Headphone/Line plugged in");

            /*Write your own condition.*/

            break;

        case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:

            NSLog(@"Headphone/Line was pulled.");

            /*Write your own condition.*/                
            break;

        case AVAudioSessionRouteChangeReasonCategoryChange:
            // called at start - also when other audio wants to play

            break;
    }
}