9

I have an requirement that to handle headphone play/pause button events in foreground. How ever I am able to handle the same scenario in background using the below code

if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
    [self becomeFirstResponder];
    NSLog(@"Responds!");
}

Please help with an explanation or sample code if possible. I have done lots of research but no help.

Santosh Gurram
  • 987
  • 3
  • 12
  • 22

3 Answers3

13

There is another way to implement player control from headphones. use MPRemoteCommandCenter tooglePlayPauseCommand. Apple documentation

[[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand addTarget:self action:@selector(onTooglePlayPause)];
slobodans
  • 723
  • 1
  • 16
  • 22
12

You must check this criteria:

  1. Edit your info.plist to stipulate that you do audio (UIBackgroundModes) in the background as well as foreground.
  2. Implement this function:

    - (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent 
    {
      if (theEvent.type == UIEventTypeRemoteControl)
      {
        switch(theEvent.subtype) {
        case UIEventSubtypeRemoteControlTogglePlayPause:
                //Insert code
                break;
            case UIEventSubtypeRemoteControlPlay:
                //Insert code
                break;
            case UIEventSubtypeRemoteControlPause:
                // Insert code
                break;
            case UIEventSubtypeRemoteControlStop:
                //Insert code.
                break;
            default:
                return;
        }
      }
    }
    

...obviously, replace the "//insert code" with whatever functionality is relevent in your app.

3>Finally, in order for that above function to be called, insert this in your viewDidAppear event:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    if ([self canBecomeFirstResponder]) {
        [self becomeFirstResponder];
    }

also please see this link: http://www.sagorin.org/2011/11/29/ios-playing-audio-in-background-audio/

AsyncMoksha
  • 2,148
  • 2
  • 22
  • 31
Banker Mittal
  • 1,848
  • 14
  • 25
  • 1
    thanks for your reply, however I am able to handle the events in background also I have added the UIBackgroundModes in plist but I have no idea about foreground events handling. I have tried trigging the mentioned method '- (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent ' but no luck. Can you please explain me how can we add foreground keys in plist. – Santosh Gurram Mar 15 '13 at 05:51
  • After 2 min, if press on headphone button, Music app started and play previous sound. – biloshkurskyi.ss Mar 22 '16 at 15:22
4

The swift 2 version for slobodans solution:

MPRemoteCommandCenter.sharedCommandCenter().togglePlayPauseCommand.addTarget(self, action: #selector(togglePlayStop(_:)));
SteMa
  • 2,729
  • 2
  • 22
  • 29