4

I have this in the AppDelegate (didFinishLaunching):

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

I tried handling the events in the relevant view controllers but that was spotty (some view controllers would get the events and others wouldn't, even when they were first responders). I tried subclassing UIApplication. That didn't work. Now I'm trying to subclass UIWindow and do this (see the comments):

- (void)sendEvent:(UIEvent *)event {
if (event.type == UIEventTypeRemoteControl) {
    NSLog(@"I wish this happened"); //this never happens
}
else
{
    NSLog(@"some other event"); //this does happen on any other interaction
    [super sendEvent:event];
}
}

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {

if (receivedEvent.type == UIEventTypeRemoteControl) {
    NSLog(@"got remote event"); // this never happens either
    switch (receivedEvent.subtype) {

        case UIEventSubtypeRemoteControlTogglePlayPause:
        {
            NSLog(@"handle play/pause");
            break;
        }


        default:
            break;
    }
}
}

I have tried both with and without this in the info plist:

<key>UIBackgroundModes</key>
<array>
        <string>audio</string>
</array>

Doesn't make a difference. As the Apple docs state, you can simulate remote control events with the audio controls (double tap home button and scroll to them on the bottom). When I press play or pause, it just plays audio from my iTunes library. I have tried the buttons on the Apple headphones as well. Nothing.

All I want to do is detect the play/pause button on a remote control, and handle the event. What else do I need to do to catch these events?

soleil
  • 12,133
  • 33
  • 112
  • 183

1 Answers1

9

ok, a few more hours of my life into black hole of Apple. Turns out that you can only capture remote control events after you have played audio of some kind using the AVAudioPlayer, or if you play a video file that has audio. This is not documented anywhere here.

So in my case I have to play a dummy silent audio file first before I try to capture remote control events. Hope this helps someone down the line.

(Update) Please note that the updated documentation now states the following:

To receive remote control events, your app must do three things:

  • Be the first responder. The view or view controller that presents the multimedia content must be the first responder.
  • Turn on the delivery of remote control events. Your app must explicitly request to begin receiving remote control events.
  • Begin playing audio. Your app must be the “Now Playing” app. Restated, even if your app is the first responder and you have turned on event delivery, your app does not receive remote control events until it begins playing audio.
phi
  • 10,634
  • 6
  • 53
  • 88
soleil
  • 12,133
  • 33
  • 112
  • 183
  • This was exactly my problem. Thanks a lot... would have taken forever to figure out. – Tom Quinn Oct 28 '12 at 19:16
  • Does not work in iOS 6+. I see the now playing info for my app in the lock screen but still, no events for pressing pause / play. – openfrog Jul 23 '13 at 20:32
  • And it does not make sense: If you use AVAudioPlayer there is no view controller "presenting it". it is a class that plays audio and has no superview. – openfrog Jul 23 '13 at 20:34
  • Sounds interesting and have solved my problem on iOS 7. Thanks :) – Shahab Jun 03 '14 at 12:12
  • @soleil Hey - the link doesn't seem to exist anymore. How do I make my app "Now Playing" app? Do I need to add some configuration? – A_G May 15 '17 at 07:56