An adaptive-bitrate player with Picture in Picture

System design scenarios Senior Swift 5.9+ iOS 17+ Reviewed By Alok Singh

Pick the fix

Picture in Picture never starts in your custom player. Most likely fix?

Options
Hint 1 of 1

What does the system need to keep playing outside the app?

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

Show answer

Answer: A Set the audio session category to .playback and enable the Audio, AirPlay and Picture in Picture background mode

Correct. PiP needs both.

Why not the others?
  • B Switch from HLS to MP4. The format isn't the problem.
  • C Call play() twice. It doesn't affect PiP eligibility.
  • D Use a UIImageView to show frames. PiP works with AVPlayerLayer, not images.

30-second answer

Use AVPlayer with HLS: it switches bitrate automatically based on bandwidth. Show it with AVPlayerViewController (PiP and controls come free) or an AVPlayerLayer with AVPictureInPictureController for custom UI. Set the audio session category to playback and enable the background mode for audio, AirPlay and Picture in Picture. Observe status, buffering and errors, and persist the playback position.

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

Design the video player.

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

  • AVPlayer + HLS
  • PiP setup
  • Audio session
  • State and errors

Deep dive

preferredPeakBitRate caps quality on cellular.

Restore your UI from the PiP delegate when the user returns.

Offline playback uses AVAssetDownloadURLSession.

FairPlay Streaming handles protected content.

Common mistakes

  • Building custom bitrate switching.
  • Forgetting the playback audio session, so PiP doesn't start.

Senior take & follow-ups

Seniors use the system's HLS and PiP machinery and focus on state and edge cases.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you limit data use?

    Show hint

    preferredPeakBitRate and Low Data Mode checks.

  2. How do you download for offline?

    Show hint

    AVAssetDownloadURLSession with HLS.

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