What an audio app must handle

Platform & distribution Mid-level Swift 5.0+ iOS 17+ Reviewed By Alok Singh

Pick the fix

The user unplugs their headphones while a podcast plays. What should happen?

Options
Hint 1 of 1

Which notification tells you the route changed?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Observe AVAudioSession.routeChangeNotification and pause when the reason is oldDeviceUnavailable

Correct. Apple expects playback to pause, so audio doesn't suddenly come from the speaker.

Why not the others?
  • B Keep playing through the speaker. That surprises users in public.
  • C Stop and reset to the beginning. Users lose their place.
  • D Do nothing, because the system pauses for you. The app must handle it.

30-second answer

Configure AVAudioSession with the playback category and enable the audio background mode. Handle interruptions such as calls and alarms, and route changes such as unplugging headphones, where you should pause. Publish Now Playing info and handle remote commands from the Lock Screen, Control Center and headphones, and support AirPlay and Siri where it fits.

Practise saying it · 45 s. No recording, just a timer

Explain what an audio app must handle.

Say your answer out loud, then check which of these points you covered:

  • Session category and background mode
  • Interruptions
  • Route changes
  • Now Playing and remote commands

Deep dive

Resume after an interruption only if the system says shouldResume.

MPNowPlayingInfoCenter shows title, artwork and progress.

MPRemoteCommandCenter handles play, pause, skip and scrubbing.

Downloaded audio needs offline handling and storage limits.

Common mistakes

  • Continuing to play aloud when headphones are unplugged.
  • Resuming after every interruption automatically.

Senior take & follow-ups

Seniors list session, interruptions, routes and system controls.

Follow-up questions

Think of your answer first, then open the hint.

  1. What happens when headphones are unplugged?

    Show hint

    A route change with oldDeviceUnavailable; pause.

  2. How do Lock Screen controls work?

    Show hint

    MPRemoteCommandCenter handlers.

Suggest a fix if something here is wrong or out of date.