Detecting if headset are plugged into iOS device

二次信任 提交于 2019-12-18 18:13:09

问题


There is an interactive movie on ios device. when movie starts (tap), the guy at the start of video will ask you plug headset , if plugged, then video should automatically jump straight to the story(straight go to the video-story). what should i do? and how to write a code?


回答1:


First you will have to register for AudioRoute Changes :-

AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange,
                                     audioRouteChangeListenerCallback,
                                 self);

Here You can depict the reason for changing your route :-

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                             
  }



回答2:


This could be an other way:

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){
     ....
     }
}



回答3:


First check if the device is connected to any headset.

+(BOOL)isHeadsetPluggedIn {

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

Then based on the bool value, write your own conditions. Something like below..

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

}

Also you can register a notification incase you want to know if the headset is removed when you are in the screen.

[[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;
    }
}


来源:https://stackoverflow.com/questions/10685313/detecting-if-headset-are-plugged-into-ios-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!