Detect when headphones are plugged in

Is there a way to detect when headphones are plugged in (or out)?

find someone on here that can convert this

- (BOOL)isHeadsetPluggedIn {
    AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance]   currentRoute];
    for (AVAudioSessionPortDescription* desc in [route outputs]) {
        if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
            return YES;
    }
    return NO;
}

Dave’s code with iOSKit: (untested but should work)

[code]Using AVFoundation
Dim route as AVAudioSessionRouteDescription = AVAudioSession.sharedInstance.currentRoute
Dim outputs as Foundation.NSArray = route.outputs
Dim count as integer = outputs.count
Dim portHeadphones as Text = AVStringConstant(“AVAudioSessionPortHeadphones”)

For i as integer = 0 to count-1
dim desc as new AVAudioSessionPortDescription(outputs.value(i))
If desc.portType = portHeadphones then
Return true
End if
Next
Return false[/code]

THANK YOU. Very very helpful! Is it possible to have an event when it changes status? Or should I put that in a timer and periodically check the state?

Yes an event should be possible, though slightly more involved. You will have to read here:
https://developer.apple.com/library/ios/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/HandlingAudioHardwareRouteChanges/HandlingAudioHardwareRouteChanges.html
There is a notification which needs to be registered for. I can post a project later today to show how to do this.

Ok, try this.
https://www.dropbox.com/s/dblvmqxk2sxjj7r/Headphones.xojo_binary_project?dl=0

I haven’t tested it on a device yet but it compiles and should do the trick. You will need to add in the Modules folder from iOSKit to use it (my current version doesn’t compile because of some unfinished things in GameKit so I removed it).