2

I am attempting to use the lock screen/now playing controls for my app but am not receiving any events. The documentation makes it seem pretty straight forward so I was a bit a surprised when I didn't get any results. My app needs to receive the remote control events for purposes outside of playing audio on the device. I tried to test this by just having it print out some confirmation with NSLog. Do I need to be using an audio or media framework that wasn't mentioned in the documentation? I am not receiving any warnings or errors so I don't think there should be any issues with that... Any insight concerning why the remote control events aren't registering would be greatly appreciated

Here is the relevant code:

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    //Register for lock screen controls
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    //Unregister lock screen controls
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}

- (BOOL)canBecomeFirstResponder{
    return YES;
}

//Lock screen controls
- (void)remoteControlReceivedWithEvent:(UIEvent *)event{
    NSLog(@"RECEIVED");
    if (event.type == UIEventTypeRemoteControl) {

        switch (event.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:
                NSLog(@"RECEIVED");
                break;

            case UIEventSubtypeRemoteControlPreviousTrack:
                NSLog(@"RECEIVED");
                break;

            case UIEventSubtypeRemoteControlNextTrack:
                NSLog(@"RECEIVED");
                break;

            default:
                break;
        }
    }
}
Squatch
  • 1,027
  • 14
  • 27

2 Answers2

3

In addition to what Jojas mentioned you need to make sure that you have an active audio session. Look at [AVAudioSession setActive:withFlags:error:].

Chris DeSalvo
  • 913
  • 1
  • 8
  • 10
  • Hmm ok. I was wondering if the lack of an audio session was part of the problem. – Squatch Jun 04 '12 at 13:17
  • This was the issue. My app does not play audio itself, but rather communicates with another device playing audio. I added the audio session but it would only receive remote events if the app had been the last to play some audio. I will be posting a new question about how to work around this but I wanted to thank you! – Squatch Jun 04 '12 at 15:31
  • @Squatch, Did you end up posting another question? – Moshe Sep 30 '13 at 15:52
  • @Moshe - I did. Never got an answer though! The feature wasn't vital for the project, so it ultimately did not get implemented. At this time, I do not have a clean solution. Here's the question: http://stackoverflow.com/q/10885047/1245406 – Squatch Oct 01 '13 at 14:35
1

Did you already ensure that your application every state to setup ReceivingRemoteControlEvents is fine?

You should look out for necessary protocol via UIApplicationDelegate in your AppDelegate. It seem that you need to implement in applicationWillResignActive and applicationDidBecomeActive.

However, there are Apple's document about App States and Multitasking. I think it will help you handle the application state with your purpose.

Hope it helps you !

Sakares
  • 606
  • 12
  • 31
  • I believe you're right. The code posted was all in my view controller and I didn't set these active state methods in my delegate. I will try your suggestion and report back. – Squatch Jun 04 '12 at 13:18
  • It turns out I had those methods implemented by the default AppDelegate. The problem was with my lack of an audio session. Thanks you for your help though. The documentation you linked helped a great deal when trying to understand exactly what was going on. – Squatch Jun 04 '12 at 15:28
  • I'm glad to help you. feel free to ask. – Sakares Jun 04 '12 at 16:07