Detect headset button click on iPhone SDK

后端 未结 3 878
面向向阳花
面向向阳花 2020-12-08 17:45

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

I managed to detect the volume buttons clicks using:

AudioSessionAddPropertyListener         


        
3条回答
  •  醉梦人生
    2020-12-08 18:03

    Can's answer was good, but I think it's outdated.

    Now you need to subclass UIApplication.

    code for main.m

    #import 
    #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];

提交回复
热议问题