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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With