Remote-control events let users control application multimedia through a the system transport controls or through an external accessory. If your application plays audio or video content as a feature, you might want to write the code that enables it to respond to remote-control events. These events originate either from the transport controls or as commands issued by external accessories (such as a headset) that conform to an Apple-provided specification. iOS converts these commands into UIEvent
objects that it delivers to an application. The application sends them to the first responder and, if the first responder doesn’t handle them, they go up the responder chain..
The following sections describe how to prepare your application for receiving remote-control events and how to handle them.
To receive remote-control events, the view or view controller managing the presentation of multimedia content must be the first responder. It (or some other object in the application) must also tell the application object that it is ready to receive remote-control events.
To make itself capable of becoming first responder, the view or view controller should override the UIResponder
methodcanBecomeFirstResponder
to return YES
. It should also send itself the becomeFirstResponder
at an appropriate time, such as (for view controllers) in an override of the viewDidAppear:
method. Listing 5-1 shows this call and also shows something else: The view controller calls the beginReceivingRemoteControlEvents
method of UIApplication
to “turn on” the delivery of remote-control events.
Listing 5-1 Preparing to receive remote-control events
- (void)viewDidAppear:(BOOL)animated { |
[super viewDidAppear:animated]; |
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; |
[self becomeFirstResponder]; |
} |
When the view or view controller is no longer managing audio or video, it should turn off the delivery of remote-control events and resign first-responder status, as shown in Listing 5-2.
Listing 5-2 Ending the receipt of remote-control events
- (void)viewWillDisappear:(BOOL)animated { |
[super viewWillAppear:animated]; |
[[UIApplication sharedApplication] endReceivingRemoteControlEvents]; |
[self resignFirstResponder]; |
} |
To handle remote-control events, the first responder must implement the remoteControlReceivedWithEvent:
method declared by UIResponder
. The method implementation should evaluate the subtype
of each UIEvent
object passed in and send the appropriate message to the object presenting the audio or video content. The example in Listing 5-3 sends play, pause, and stop messages to an instance of AVAudioPlayer
.
Listing 5-3 Handling remote-control events
// On application launch, configure the audio session |
// Call prepareToPlay: after initializing AVAudioPlayer |
- (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent { |
BOOL success=YES; |
if (theEvent.type == UIEventTypeRemoteControl) { |
switch(theEvent.subtype) { |
case UIEventSubtypeRemoteControlPlay: |
success = [audioPlayer play]; |
if (!success) return; |
break; |
case UIEventSubtypeRemoteControlPause: |
[audioPlayer pause]; |
break; |
case UIEventSubtypeRemoteControlStop: |
[audioPlayer stop]; |
audioPlayer.currentTime = 0; |
break; |
default: |
return; |
} |
} |
} |
Other remote-control UIEvent
subtypes are possible. See UIEvent Class Reference for details.
You can test your application’s receipt and handling of remote-control events by using the audio playback controls. These controls are available on recent models of device (for iPhone, iPhone 3GS and later) that are running iOS 4.0 or later. To access these controls, double-press the Home button, then flick left or right along the bottom of the screen until you find the audio playback controls. These controls send remote-control events to the application that is currently or was most recently playing audio; the icon to the right of the playback controls represents the application.
For testing purposes, you can programmatically make your application begin audio playback and then test the remote-control events sent to your application by tapping the playback controls. Note that a deployed application should not programmatically begin playback; that should always be done by the user.
Last updated: 2010-08-03