4

I am using the AVAudioPlayer class to play .mp3 files in my App. Is it possible to check whether the earphones are connected to the device programmatically?

In first generation iPod devices, the volume control bar is hidden (for music and videos) when no earphones are connected.

unwind
  • 391,730
  • 64
  • 469
  • 606
Biranchi
  • 16,120
  • 23
  • 124
  • 161
  • http://stackoverflow.com/questions/667196/detecting-iphone-ipod-touch-accessories – Les Nov 11 '09 at 09:45

3 Answers3

5

You can get the current audio 'route' by calling AudioSessionGetProperty with the kAudioSessionProperty_AudioRoute property. This gives you a string such as "Headphone" or "Speaker".

You can also use AudioSessionAddPropertyListener to listen for when the route changes (eg. disconnecting headphones)

See the apple docs here

Alex Deem
  • 4,717
  • 1
  • 21
  • 24
2

AudioSessionGetProperty and AudioSessionAddPropertyListener are deprecated in iOS 7.

Instead, use: AVAudioSessionRouteChangeNotification

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

Listener,

-(void)handleRouteChange:(NSNotification*)notification 
{
    NSInteger reason = [[[notification userInfo] objectForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
    switch (reason) {
        case AVAudioSessionRouteChangeReasonOldDeviceUnavailable :
            break;
        case AVAudioSessionRouteChangeReasonNewDeviceAvailable :
            break;
        case AVAudioSessionRouteChangeReasonOverride :
            break;
        case AVAudioSessionRouteChangeReasonCategoryChange :
            break;
        case AVAudioSessionRouteChangeReasonWakeFromSleep :
            break;
        case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory :
            break;
        case AVAudioSessionRouteChangeReasonRouteConfigurationChange :
            break;
        case AVAudioSessionRouteChangeReasonUnknown:
        default:
            break;
    }
}
David James
  • 2,430
  • 1
  • 26
  • 35
  • Sometimes, the AVAudioSession notification does not calling in main queue. So that wrapping your method inside a block will avoid related issue (update UI...): `dispatch_async(dispatch_get_main_queue(), ^{ // Your method });` – nahung89 Aug 11 '15 at 04:42
  • @nahung89 If the notification is not calling in main queue what is advantage of using block in main queue? – Fa.Shapouri Oct 04 '17 at 07:49
  • @Fa.Shapouri as I pointed out in my above comment, you should read carefully. – nahung89 Oct 04 '17 at 09:05
0

There are two approaches:

1) check the instantaneous state of the audio route

Detect if headphones (not microphone) are plugged in to an iOS device

This answer furnishes you with a ready-made method for detecting whether headphones are plugged in.

2) monitor route change events, and set a flag whenever the route changes between headset and non-headset

How to programmatically detect earpiece in iphone?

( would probably want to add the code from the first link into this to actually determine whether headset/non-headset status has been changed within the route change callback )

Community
  • 1
  • 1
P i
  • 29,020
  • 36
  • 159
  • 267