Support controlling background audio from the Control Center and iOS Lock screen.
If your app plays audio in the background, support remotely controlling playback both in Control Center and from the iOS Lock screen. Along with controlling playback, also provide meaningful information in these interfaces about what’s currently playing. To implement this functionality, you use the Media Player framework’s
Configure the Remote Command Handlers
MPRemote class configures objects used for handling remote-control events sent by external accessories and system transport controls. It defines a variety of commands in the form of
MPRemote objects to which you can attach custom event handlers to control playback in your app. For instance, to remotely control your app’s play and pause behaviors, you get a reference to the shared command center and provide handlers for the appropriate commands.
The preceding example shows how to add handlers for the command center’s
pause using the
add method. The callback block you give to this method requires that you return a
MPRemote value, indicating whether the command succeeded or failed.
Provide Display Metadata
After you’ve configured your remote command handlers, the next step is to provide metadata to display on the iOS Lock screen and in Control Center’s transport area. You provide a dictionary of metadata using the keys defined by
MPNow and set that dictionary on the default instance of
MPNow. The following example shows you how to set a title and artwork for the currently presented media, along with playback timing values:
This example passes dynamic timing as part of the
now dictionary, which allows you to present player timing and progress in the remote-control interfaces. When doing so, provide an up-to-date snapshot of this timing as your app enters the background. You can also update the
now while your app is in the background, if media information or timing changes significantly.