Detecting iPhone/iPod Touch Accessories
Asked Answered
F

5

10

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.

Fuzee answered 20/3, 2009 at 17:41 Comment(0)
F
10

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.

Fuzee answered 20/3, 2009 at 22:43 Comment(1)
For some reason this does not work for me. On an iPod Touch 2nd gen without headphones connected it returns TRUE...Molar
S
4

Or you could use:

if (![[AVAudioSession sharedInstance] inputIsAvailable]) {
    // your code here for no audio input available
}
Strong answered 28/12, 2010 at 15:36 Comment(0)
M
4

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

BOOL audioHWAvailable = audioSession.inputAvailable;
Memorize answered 24/9, 2012 at 15:28 Comment(1)
If you need to test for audio recording, this boolean value will allow you to check for availability of audio recording without stopping existing audio playback.Remiss
H
0

To determine if the device has a built in microphone you can just go by [UIDevice currentDevice].model to see if it's an iPhone or a 2nd generation iPod Touch. As far as a third-party microphone plugged into the dock connector, this is not possible in the current 2.2.1 SDK, but it may be in a later version :)

Heedful answered 20/3, 2009 at 19:4 Comment(1)
It's better to test for feature support separate from device type.Hintze
C
0

Here is the solution, you may like it or it is helpful to you.

Before using below method please write this two line also

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

- (void)isHeadsetPluggedIn {

    UInt32 routeSize = sizeof (CFStringRef);
    CFStringRef route;

    AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
                                              &routeSize,
                                              &route);

    //NSLog(@"Error >>>>>>>>>> :%@", error);
    /* Known values of route:
     * "Headset"
     * "Headphone"
     * "Speaker"
     * "SpeakerAndMicrophone"
     * "HeadphonesAndMicrophone"
     * "HeadsetInOut"
     * "ReceiverAndMicrophone"
     * "Lineout"
     */

    NSString* routeStr = (NSString*)route;

    NSRange headsetRange = [routeStr rangeOfString : @"Headset"];
    NSRange receiverRange = [routeStr rangeOfString : @"Receiver"];

    if(headsetRange.location != NSNotFound) {
        // Don't change the route if the headset is plugged in.
        NSLog(@"headphone is plugged in ");
    } 
    else if (receiverRange.location != NSNotFound) {
        // Change to play on the speaker
        NSLog(@"play on the speaker");

    } 
    else {
        NSLog(@"Unknown audio route.");

    }
}
Caveator answered 27/9, 2011 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.