Remote Control Events

Remote control events are a way for users to control your app’s multimedia. Remote control events originate from external accessories or from the transport controls displayed by the system and are delivered to your app through the classes of the Media Player framework. Apps that play audio or video content use these events to start and stop playback, change tracks, and even rate an item. All media apps should support these events.

In addition to supporting remote control events, apps can use the Media Player framework to provide playback information for tracks. The system displays playback information in appropriate places, such as the lock screen and in Control Center.

For more information about the classes of the Media Player framework, see Media Player Framework Reference.

Preparing Your App for Remote Control Events

To receive remote control events, do the following:

If your app also provides Now Playing information containing information about the current track, use an MPNowPlayingInfoCenter object to update that information at appropriate times. For more information about how to provide now playing information, see Providing Now Playing Information.

Handling Remote Control Events

To handle a particular remote control event, register an action handler with the appropriate MPRemoteCommand object. The Media Player framework defines a standard set of MPRemoteCommand objects for handling media-related events. When an accessory or iOS interface generates a remote control event, the system notifies the corresponding MPRemoteCommand object. That object responds by executing any attached handlers.

You retrieve remote command objects from the shared MPRemoteCommandCenter object. The properties of the remote command center contain objects for managing playback, switching tracks, and for rating the track. Fetch the object you want to support and attach a handler using either a block or an action method. Typically, you register a single handler for each type of command but you can register multiple handlers as needed. Listing 5-1 shows how to register a block to be executed when the user wants to play a track. In the example, the block plays the currently selected track using a custom player object. In your code, perform whatever actions you require to play the track.

Listing 5-1  Registering a remote control event handler

MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.playCommand addTargetUsingBlock:^(MPRemoteCommandEvent *event) {
    // Begin playing the current track.
    [[MyPlayer sharedPlayer] play];

If you explicitly do not want to support a given command, fetch the command object and set its enabled property to NO. Disabling a remote command lets the system know that it should not display any related UI for that command when your app is the Now Playing app.

Some command objects let you specify other information related to the command. For example, feedback commands let you specify a localized string that describes the meaning of the feedback. Be sure to look at the reference documentation for information about the specific command you are supporting.

For more information about adding and removing handlers for remote control events, see MPRemoteCommand Class Reference.

Providing Now Playing Information

When playing audio or video content, use the MPNowPlayingInfoCenter object to provide descriptive information about the content being played. The MPNowPlayingInfoCenter object contains a nowPlayingInfo dictionary whose contents describe the item being played. The system displays the information from that dictionary in appropriate places, such as on the lock screen of an iOS device.

Listing 5-2 shows an example of how to use information in an MPMediaItem object to configure the now playing information. The example enumerates over a fixed set of properties and uses the resulting values to build a new dictionary that is then assigned to the MPNowPlayingInfoCenter object.

Listing 5-2  Gathering the Now Playing info for a media item

- (void)configureNowPlayingInfo:(MPMediaItem*)item
    MPNowPlayingInfoCenter* info = [MPNowPlayingInfoCenter defaultCenter];
    NSMutableDictionary* newInfo = [NSMutableDictionary dictionary];
    NSSet* itemProperties = [NSSet setWithObjects:MPMediaItemPropertyTitle,
    [item enumerateValuesForProperties:itemProperties
          usingBlock:^(NSString *property, id value, BOOL *stop) {
        [newInfo setObject:value forKey:property];
    info.nowPlayingInfo = newInfo;

For more information about the types of information you can provide, see MPNowPlayingInfoCenter Class Reference.

Testing Remote Control Events on a Device

Test that your app is properly receiving and handling remote control events with Control Center, which you access by swiping up from the bottom edge of your screen. These controls send remote control events to the app that is currently or was most recently playing audio. You can also access the playback controls from the lock screen of the device.