4

How can I use the Lock Screen iPod Controls for my own App?

I tried MPNowPlayingInfoCenter, but if I set the information it won't be displayed anywhere; Not on the lock-screen and not in airplay on AppleTV.

I use the AVPlayer to play my audio files.

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
Jeanette Müller
  • 999
  • 1
  • 11
  • 19

1 Answers1

12

Take a look at the Remote Control of Multimedia documentation.

Here’s how to listen for remote control events in a UIViewController subclass. First, make your controller participate in the responder chain, otherwise the events will be forwarded to the app delegate:

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

Where appropriate, tell the application to start receiving events and make your controller the first responder:

// maybe not the best place but it works as an example
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

Then respond to them:

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

    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:
                [self playOrStop: nil];
                break;

            case UIEventSubtypeRemoteControlPreviousTrack:
                [self previousTrack: nil];
                break;

            case UIEventSubtypeRemoteControlNextTrack:
                [self nextTrack: nil];
                break;

            default:
                break;
        }
    }
}
Ryder Mackay
  • 2,900
  • 18
  • 29
  • 1
    what's not obvious here is that viewDidAppear should be declared in the viewController, whilst remoteControlReceivedWithEvent should be declared in the app delegate. – unsynchronized Jun 30 '12 at 09:43
  • That’s not necessary, but was true in my previous example. I forgot to convey a subtle point from the documentation: `UIViewController` can participate in the responder chain, but only after overriding `-(BOOL)canBecomeFirstResponder` to return `YES`. See my edit. – Ryder Mackay Jun 30 '12 at 18:53