user1369476
user1369476

Reputation: 23

Detecting if headset are plugged into iOS device

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?

Upvotes: 2

Views: 4960

Answers (3)

Pradeep Reddy Kypa
Pradeep Reddy Kypa

Reputation: 4022

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

Upvotes: 0

Balazs Nemeth
Balazs Nemeth

Reputation: 2332

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

Upvotes: 0

Abhishek Singh
Abhishek Singh

Reputation: 6166

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                             
  }

Upvotes: 3

Related Questions