Possible Duplicate:
How can I find out if an external headset is connected to an iPhone?
I'm developing an audio-player application and in one of modes(playing mode) i want to detect the status of headset, if the jack is plugged in it should plays else shouldn't.
EDIT Below is a snippet from my code, there are explanations too about what I'm trying to do. I found most of code from other threads on SO, but still can't make it run as how I want.
- (void)isHeadsetPluggedIn {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;
AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route);
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 ");
//here is the place i want to play music
/* AVAudioPlayer *mySound;
mySound =[[AVAudioPlayer alloc]initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"apple_SIMToolkitPositiveACK" withExtension:@"mp3" ]error:nil];
[mySound play];
//here is the place where i must receive the info about plug-in(headset)
*/
}
else if (receiverRange.location != NSNotFound) {
// Change to play on the speaker
NSLog(@"play on the speaker");
}
else {
NSLog(@"Unknown audio route.");
}
}