Allowing your app to receive remote audio control events is pretty easy.
The gist of it is to just tell the OS you’re interested in being told of the events. Then you just have a callback for the events and you can act on them.
For the view controller that’s playing music, you can declare interest in receiving the events in the viewWillAppear: method like this…
- (void)viewDidAppear:(BOOL)animated { |
[super viewDidAppear:animated]; |
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; |
[self becomeFirstResponder]; |
} |
Do the reverse in the viewWillDisappear: like this…
- (void)viewWillDisappear:(BOOL)animated { |
[[UIApplication sharedApplication] endReceivingRemoteControlEvents]; |
[self resignFirstResponder]; |
[super viewWillDisappear:animated]; |
} |
Then you’re ready to handle the events…
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent { |
if (receivedEvent.type == UIEventTypeRemoteControl) { |
switch (receivedEvent.subtype) { |
case UIEventSubtypeRemoteControlTogglePlayPause: |
[self togglePlayPause]; |
break; |
case UIEventSubtypeRemoteControlPreviousTrack: |
[self playPrevTrack]; |
break; |
case UIEventSubtypeRemoteControlNextTrack: |
[self playNextTrack]; |
break; |
default: |
break; |
} |
} |
} |
See details here in Apple documentation.