Detecting if headset are plugged into iOS device

4.9k views Asked by At

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?

3

There are 3 answers

4
Abhishek Singh On BEST ANSWER

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                             
  }
0
Balazs Nemeth On

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){
     ....
     }
}
0
Pradeep Reddy Kypa On

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