Search code examples
iosxcodeheadphones

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?


Solution

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