Detect headset button click on iPhone SDK

MCO picture MCO · Sep 15, 2011 · Viewed 9.7k times · Source

Is there a way to detect the headset's play/pause button click?

I managed to detect the volume buttons clicks using:

AudioSessionAddPropertyListener( kAudioSessionProperty_CurrentHardwareOutputVolume , audioVolumeChangeListenerCallback, self );

But I can't find an AudioSessionProperty for the center button. What's the way to do that?

Answer

Can picture Can · Sep 18, 2011

Everything that's done from outside your app is considered a "Remote Event". If you double-tap the Home button and press Play/Pause there, it's the equivalent of pressing the play/pause button on the headset (Same for double tapping for next, and triple tapping for previous).

Here's the guide on event handling of remote events for iOS.

Personally, I like subclassing the MainWindow (UIWindow) and overriding the sendEvent: method, so I can manage it more directly:

- (void)sendEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl)
    {
        // Do stuff here
    }
    else
    {
        // Not my problem.
        [super sendEvent:event];
    }
}

Hope that helps, the enum for the event of the central button is UIEventSubtypeRemoteControlTogglePlayPause.