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?
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
.
Can's answer was good, but I think it's outdated.
Now you need to subclass UIApplication.
code for main.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "MyUIApplication.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(
argc,
argv,
NSStringFromClass([MyUIApplication class]),
NSStringFromClass([AppDelegate class]));
}
}
Code for MyUIApplication.m
:
@implementation MyUIApplication
- (void)sendEvent:(UIEvent *)event {
if (event.type == UIEventTypeRemoteControl) {
// Check event.subtype to see if it's a single click, double click, etc.
} else {
// Not my problem.
[super sendEvent:event];
}
}
@end
Code for AppDelegate.m
:
inside of - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
call [application beginReceivingRemoteControlEvents];
来源:https://stackoverflow.com/questions/7428783/detect-headset-button-click-on-iphone-sdk