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

耗尽温柔 提交于 2019-12-01 13:31:33

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

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.

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.

From SpeakHere

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!