Headset Button Detection on iPhone SDK

Is there any way to detect the play / pause button in the headset?

I managed to detect buttons with volume buttons using:

AudioSessionAddPropertyListener( kAudioSessionProperty_CurrentHardwareOutputVolume , audioVolumeChangeListenerCallback, self ); 

But I can not find AudioSessionProperty for the center button. How to do it?

+9
source share
3 answers

Everything that is done outside of your application is considered a "remote event". If you press the Home button twice and press Play / Pause there, it is equivalent to pressing the play / pause button on the headset (Same thing for double-clicking for the next and triple pressing for the previous one).

Here is a guide to handling remote event events for iOS .

Personally, I like to subclass MainWindow ( UIWindow ) and override the sendEvent: method, so I can control it more directly:

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

Hope this helps, the listing for the center button event is UIEventSubtypeRemoteControlTogglePlayPause .

+8
source

The answer may be good, but I think it is out of date.

Now you need to subclass UIApplication.

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 a single click, double click, etc. } else { // Not my problem. [super sendEvent:event]; } } @end 

Code for AppDelegate.m :

inside - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

call [application beginReceivingRemoteControlEvents];

+1
source

I tried everything above, but, unfortunately, now nothing seems to work. Then I looked at beginReceivingRemoteControlEvents and found that it

On iOS 7.1 and later, use the MPRemoteCommandCenter shared object to record remote control events. You do not need to call this method when using the common object of the command center.

Then I checked MPRemoteCommandCenter and finally ended up on the MPRemoteCommand documentation MPRemoteCommand .

It’s good that there is such an example:

 let commandCenter = MPRemoteCommandCenter.shared() commandCenter.playCommand.addTarget(handler: { (event) in // Begin playing the current track self.myMusicPlayer.play() return MPRemoteCommandHandlerStatus.success }) 

Now, if we want to read the middle button, we can do:

 MPRemoteCommandCenter.shared().togglePlayPauseCommand.addTarget { (event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus in // middle button (toggle/pause) is clicked print("event:", event.command) return .success } 

This works, and I managed to detect the middle button of the headphones.

Note: I noticed that there is another behavior that depends on where we placed such code above. That is, when I put the View Controller, the reported events are identical, and when I put it in AppDelegate, the didFinishLaunching of the reported events are different. In any case, the event is detected.

0
source

Source: https://habr.com/ru/post/897377/


All Articles