Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting iPhone/iPod Touch Accessories

Is it possible to detect if the iPod Touch/iPhone has any headphones or other accessories connected to it?

I'm building an app that requires a microphone, and need to know if the "iSomething" has one connected or not, either via the dock connection, or using the headphone port, such as with the inline headphone/microphone accessory from Apple.

like image 772
Josh Buhler Avatar asked Mar 20 '09 17:03

Josh Buhler


3 Answers

Finally found it - After initializing the Audio Session object, - AudioSessionInitialize() - you can make a call to AudioSessionGetProperty, and get the value of kAudioSessionProperty_AudioInputAvailable.

AudioSessionInitialize(NULL, NULL, NULL, NULL);    
UInt32 propertySize, micConnected;
    AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &propertySize, &micConnected);
    [self updateMicStatus:micConnected]; // user-created method

According to the docs for Audio Session Services, this should be used rather than using the device model (iPhone vs. iPod Touch) to determine if an audio input is available to use. You can also set up a callback function to monitor changes to this property via AudioSessionAddPropertyListener().

Not sure yet if this property also applies to devices connected via the Dock connector, but it appears to work for the headphone jack.

like image 171
Josh Buhler Avatar answered Oct 09 '22 12:10

Josh Buhler


Or you could use:

if (![[AVAudioSession sharedInstance] inputIsAvailable]) {
    // your code here for no audio input available
}
like image 41
Bassem Tamimi Avatar answered Oct 09 '22 10:10

Bassem Tamimi


In IOS 6 inputIsAvailable is deprecated. In the future we need to use inputAvailable:

BOOL audioHWAvailable = audioSession.inputAvailable;
like image 22
Prine Avatar answered Oct 09 '22 12:10

Prine