Background Audio Player Sync Control Center

Quang Quoc Tran
3 min readFeb 11, 2019

--

In this tutorial, you will create a player with capability of play audio in background mode and synchronize with music controls in Control Center.

Features:

  1. Play a sound file.
  2. Make the sound continue playing in background mode.
  3. Control the audio from Control Center.
  4. Handle finish playing.
  5. Handle interruptions.
  6. Handle route changes.

Before go into every feature, you can download the full source code project here if you stuck at anywhere.

1. Play a Sound File

To play a sound file, you need to initializae a player. Add the following block code into your view controller to initialize the player:

To play the mp3 file, you call play() method of the player as follows:

player.play()

Now you can hear the sound from the audio file that I imported “song.mp3”. But when you press Home button to move the app into background mode then the sound has been muted. So we need to allow it continue playing in background mode in next guide.

2. Make the Sound Continue Playing in Background Mode

Now when you press Home button then the sound will be muted. So to allow the sound continue playing in background mode then you need to enable this capability in Signing & Capabilities tab as figure shown below:

In addition, you also replace application(_:didFinishLaunchingWithOptions:) method in AppDelegate.swift file as follows to config audio session:

Now run the app, play the sound and press Home button to move the app into background mode and check if the sound still continue playing.

3. Control the Audio from Control Center

Read the following article to know how to controll background audio from the Control Center and iOS Lock screen:

Controlling Background Audio
Support controlling background audio from the Control Center and iOS Lock screen.

Add the following methods into your view controller and call setupRemoteTransportControls(), setupNowPlaying() methods from viewDidLoad() method:

Now run the app, open Control Center and check it.

4. Handle Finish Playing

After audio player finished playing, you need to update nowPlayingInfo and UI. To receive this event you need to implement audioPlayerDidFinishPlaying(_:successfully:) method of AVAudioPlayerDelegate protocol and update setUpPlayer() method as follows:

Run the app and check it.

5. Handle Interruptions

When you are playing in background mode, if a phone call come then the sound will be muted but when hang off the phone call then the sound does not automatically continue playing. So we need to handle these interruption events by reading the following article:

Responding to Audio Session Interruptions
Directly observe audio session notifications to ensure that your app responds to interruptions.

Add the following code snippet for autoplay when the interruption ended:

Now run the app and test it.

6. Handle Route Changes

Now when you plug a headphone into the phone then the sound will emit on the headphone. But when you unplug the headphone then the sound automatically continue playing on built-in speaker. Maybe this is the behavior that you don’t expect. B/c when you plug the headphone into you want the sound is private to you, and when you unplug it you don’t want it emit out to other people. We will handle it by receiving events when the route change.

At first you need to read this article to understand about route changes:

Responding to Audio Session Route Changes
Directly observe audio session notifications to ensure that your app responds to interruptions.

Update setupNotifications() method and add handleRouteChange(: Notification) method as follows:

Run the app and test it.

That is all. You can download the full source code project here

Please Support Me!

If this post is useful, please support me by trying my free Unimal app. Thanks for your support!

--

--