How can I detect if headphones are connected to an iPod touch G1?
Asked Answered
V

5

1

There are many articles on how to detect if a microphone is connected to an iPod touch G2 via AudioSessionGetProperty / kAudioSessionProperty_AudioInputAvailable, but I have not seen any articles related to detection of headphones connected to an iPod touch G1.

To review: iPod touch G2 hardware differs from iPod touch G1 hardware in the following ways:

  • iPod touch G2 has an internal speaker
  • iPod touch G2 is able to use microphone off of headphone port

I have an app that needs to play sound to be useful and I want to be nice and have a detector that shows that the app is useful once they connect up some headphones.

My initial trials show that the AudioSession APIs (and specifically the AudioSessionGetProperty with the kAudioSessionProperty_AudioRoute constant) always reports back 'Headphone' even if headphones are not connected to an iPod touch G1.

Am I missing something? Do I have something cross wired with my AudioSession calls? If anyone has tried this on an iPod touch G1 and got a different result? Is there another way to weave through AudioSession APIs and get what I am after?

This is all against iPhone OS 3.0 and the iPhone OS 3.0 SDK on real iPod touch G1 hardware.

Thanks in advance, --Batgar

Vienne answered 6/8, 2009 at 13:8 Comment(0)
D
2

you can easily get with this method:

- (BOOL)isHeadsetPluggedIn {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;

OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route);

if (!error && (route != NULL) && ([route isEqual:@"HeadsetInOut"])) {
return YES;
}

return NO;
}
Drunkometer answered 28/3, 2012 at 15:35 Comment(2)
This method does not compile. Bad receiver type 'CFStringRef' (aka 'const struct __CFString *')Gamopetalous
@Sabobin: No, You have done something wrong ,because this method is tested and i used same code in one of my project..... So please check once and fix your problem.....hope so you get success...!Drunkometer
S
1

Check it.

http://developer.apple.com/iphone/library/samplecode/SpeakHere/index.html#//apple_ref/doc/uid/DTS40007802

Here is the source code of sound recording which have support for pausing playback when headphones are removed, this may help you.

Swell answered 31/10, 2009 at 5:6 Comment(0)
S
1

The kAudioSessionProperty_AudioRoute will always return headphones in the 1st gen since there are no other routes. The 2nd gen and iphone and above all will support another route (speaker) when the headphones are unplugged, but there's note another route in the 1st gen.

At least with this documented API call you are using, you will not be able to detect the 1st gen ipod headphone state.

Skeie answered 31/10, 2009 at 5:32 Comment(2)
The core issue is that it always reports back 'Headphone' even if no headphones are plugged in.Vienne
Correct. That's what I'm saying. The method doesn't tell you if headphones are plugged in or not. It tells you where the sound is being routed. Since this device has no internal speaker, it's always routing the sound to the headphones - even if there are no headphones connected.Skeie
V
0

From SpeakHere

error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable); if (error) printf("ERROR GETTING INPUT AVAILABILITY! %d\n", error); btn_record.enabled = (inputAvailable) ? YES : NO;

Virtuosity answered 1/1, 2010 at 10:29 Comment(0)
M
0

Above answer does not work as it Does not compile , So i am posting this as this might help some one . All you need to do is to find the audio route . Following are the possible routes for the audio

Known values of route:

  • "Headset"
  • "Headphone"
  • "Speaker"
  • "SpeakerAndMicrophone"
  • "HeadphonesAndMicrophone"
  • "HeadsetInOut"
  • "ReceiverAndMicrophone"
  • "Lineout"

Hope this helps

  - (BOOL)isHeadsetPluggedIn {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;

OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
                                          &routeSize,
                                          &route);



if (!error && (route != NULL)) {

    NSString* routeStr = (__bridge NSString*)route;  //Convert CFStringRef to NSString
    NSRange routeRange = [routeStr rangeOfString:@"Head"];
    if (routeRange.location != NSNotFound){
         return YES;
    }

}
    return NO;
}
Mackinnon answered 2/7, 2014 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.