Enable background audio in your app to provide a seamless playback experience.
While most watchOS apps are optimized for quick interactions, apps that play audio content should continue playing even after the user lowers their wrists or navigates to a new app. Your app needs to play audio in the background in order to provide this seamless playback experience.
To play background audio:
Enable the Audio Background Mode.
Configure and Activate the audio session.
Enable the Audio Background Mode
First, you must enable the Audio Background Mode capability for your WatchKit extension, as shown in Figure 1.
This step sets the UIBackgroundModes key in your extension’s
Configure and Activate the Audio Session
Before you can play audio, you need to set up and activate the audio session.
Next, activate the session, by calling the
This method sets up the audio route asynchronously before activating your session. watchOS requires a Bluetooth audio route for long-form audio. If necessary, the system presents an audio route picker to the user, letting them choose the Bluetooth route (see Figure 2).
In general, if the user has previously selected a Bluetooth route or if AirPods or other W1-equipped Bluetooth headphones are nearby, the system picks the audio route automatically without displaying a picker view to the user. If no applicable Bluetooth route is selected (either automatically or by the user), the system passes an error to the completion handler.
activate(options: method calls its completion handler as soon as a Bluetooth route is selected or when an error occurs. Check for errors in the completion handler. If no errors occurred, you can begin playing your audio content.
The code listing below shows all the steps needed to set up the session, activate it, and begin playing.