Domanda

I want to detetct wether audio Jack is plugged in or not in my iPhone/iPad device.
I have tried many code Like C function available on stackOverflow but it is not working
tell me code which compitible with iOS 5.0 to 7

if there is any tutorial/code for it? then tell me

È stato utile?

Soluzione

After lots of googling i find the best & Simple solution to detect Audio Jack

first of all add following method in your code

- (BOOL)areHeadphonesPluggedIn {
    NSArray *availableOutputs = [[AVAudioSession sharedInstance] currentRoute].outputs;
    for (AVAudioSessionPortDescription *portDescription in availableOutputs) {
        NSLog(@"%@",portDescription.portType);
        if ([portDescription.portType isEqualToString:AVAudioSessionPortHeadphones]) {
            return YES;
        }
    }
    return NO;
}

then test it like

if ([self areHeadphonesPluggedIn] == TRUE) {
    NSLog(@"Device Detetcted.  Headphone PluggedIn");
}
else {
    NSLog(@"Device Not Detetcted.  Headphone PluggedOut");
}

Very Simple!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top