I'd like to make my app use the audio buttons on the lock screen while multitasking. (Yes, like Pandora.) What API am I looking to use?
See the Remote Control of Multimedia docs. Basically, you just need to call -beginReceivingRemoteControlEvents
on your shared application instance, then register something (probably your main view controller) as the first responder and implement the -remoteControlReceivedWithEvent:
method on it. You will get events both from the lock-screen controls and from the headset clicker, as well as the control buttons to the left of the multitasking drawer. To play audio while your application isn't foremost, you should also check out this information on background audio.
It is even easier now, as of iOS 7. Here's the example for the play/pause toggle (headset button). See the docs for MPRemoteCommandCenter and MPRemoteCommand for more options.
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"toggle button pressed");
return MPRemoteCommandHandlerStatusSuccess;
}];
or, if you prefer to use a method instead of a block:
[commandCenter.togglePlayPauseCommand addTarget:self action:@selector(toggleButtonAction)];
To stop:
[commandCenter.togglePlayPauseCommand removeTarget:self];
or:
[commandCenter.togglePlayPauseCommand removeTarget:self action:@selector(toggleButtonAction)];
You'll need to add this to the includes area of your file:
@import MediaPlayer;
© 2022 - 2024 — McMap. All rights reserved.