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?
Tried all above but sadly now none seems to work. Then I took a peek at
beginReceivingRemoteControlEvents
and found thisThen checked out
MPRemoteCommandCenter
and finally ended up inMPRemoteCommand
documentation page.Good thing is there is this example:
Now if we want to read middle button we can do:
This works and I managed to get the headphone's middle button detected.
Note: I noticed there is different behaviour that depends on where we put such code above. That is when I put in View Controller the reported events are identical and when I put it in AppDelegate's didFinishLaunching the reported events are different. Either way the event is detected.
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 thesendEvent:
method, so I can manage it more directly:Hope that helps, the enum for the event of the central button is
UIEventSubtypeRemoteControlTogglePlayPause
.Can's answer was good, but I think it's outdated.
Now you need to subclass UIApplication.
code for
main.m
Code for
MyUIApplication.m
:Code for
AppDelegate.m
:inside of
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
call
[application beginReceivingRemoteControlEvents];