RORK LABJP
FUNDING — Rork raised a $15M seed led by Left Lane Capital, with Peak XV, True Ventures, Goodwater, and a16z Speedrun joiningENGINE — Rork Max runs on Claude Code and Claude Opus 4.6; it drew 8M+ views on X and doubled annual revenue in two weeksSWIFT — Rork Max is the first web-based Swift app builder, positioned to replace Apple's traditional XcodePRODUCT — Rork Max covers the whole Apple ecosystem: iPhone, iPad, Apple Watch, Apple TV, Vision Pro, and iMessageCLASSIC — The original Rork uses React Native (Expo), building iOS/Android apps from a plain-English descriptionPRICING — Start free; paid plans begin at $25/mo, and Rork Max is $200/moFUNDING — Rork raised a $15M seed led by Left Lane Capital, with Peak XV, True Ventures, Goodwater, and a16z Speedrun joiningENGINE — Rork Max runs on Claude Code and Claude Opus 4.6; it drew 8M+ views on X and doubled annual revenue in two weeksSWIFT — Rork Max is the first web-based Swift app builder, positioned to replace Apple's traditional XcodePRODUCT — Rork Max covers the whole Apple ecosystem: iPhone, iPad, Apple Watch, Apple TV, Vision Pro, and iMessageCLASSIC — The original Rork uses React Native (Expo), building iOS/Android apps from a plain-English descriptionPRICING — Start free; paid plans begin at $25/mo, and Rork Max is $200/mo
Articles/Dev Tools
Dev Tools/2026-06-24Advanced

Audio Apps That Survive Calls, Unplugged Headphones, and Other Apps Taking Over

How to handle AVAudioSession interruption and route-change notifications correctly so playback survives calls and headphone unplugs, with working Swift code.

Rork Max184AVFoundationAudioSwiftUI53Background Playback

Premium Article

When I shipped a small app, built as an indie developer, that plays meditation and study audio, the very first bug report read: "after a phone call, the sound doesn't come back." It is natural for audio to stop when a call comes in mid-playback, but even after the call ended, the app stayed paused, with the on-screen play button frozen in its "playing" look.

The cause was that I was not handling AVAudioSession interruptions at all. Playback was left entirely to AVAudioPlayer, and my code was not listening to the OS telling it "I've interrupted you now" and "you may resume." I learned the hard way that the quality of an audio app is decided less by whether the features run and more by how carefully it answers this kind of interruption.

This article walks through handling interruptions from calls and Siri, and route changes from plugging and unplugging headphones, in a form you can drop straight into the native Swift that Rork Max generates.

First, decide the audio session category

Before anything else, declare to the OS what kind of audio app this is. Skip it and your sound may vanish with the silent switch, or you may needlessly stop another app's music.

import AVFoundation
 
func configureAudioSession() {
    let session = AVAudioSession.sharedInstance()
    do {
        // A playback-first app: plays regardless of the silent switch
        try session.setCategory(.playback, mode: .default)
        try session.setActive(true)
    } catch {
        print("Audio session setup failed: \(error)")
    }
}

.playback declares "this app's sound is the content itself," letting it play while locked or in the background. Conversely, if you only want to layer a short effect over another app's music, you would choose .ambient. Choosing the right declaration up front is the foundation for every later behavior.

Subscribe to interruption notifications

Interruptions arrive via AVAudioSession.interruptionNotification. There are two phases — .began and .ended — and the end carries a hint about whether you may resume on your own.

final class PlaybackController {
    private var wasPlayingBeforeInterruption = false
 
    func observeInterruptions() {
        NotificationCenter.default.addObserver(
            self, selector: #selector(handleInterruption),
            name: AVAudioSession.interruptionNotification, object: nil)
    }
 
    @objc private func handleInterruption(_ note: Notification) {
        guard let info = note.userInfo,
              let raw = info[AVAudioSessionInterruptionTypeKey] as? UInt,
              let type = AVAudioSession.InterruptionType(rawValue: raw) else { return }
 
        switch type {
        case .began:
            // A call or Siri broke in. Remember the state.
            wasPlayingBeforeInterruption = player.isPlaying
            player.pause()
 
        case .ended:
            guard let optsRaw = info[AVAudioSessionInterruptionOptionKey] as? UInt else { return }
            let options = AVAudioSession.InterruptionOptions(rawValue: optsRaw)
            // Resume only if shouldResume is set AND we were playing before
            if options.contains(.shouldResume), wasPlayingBeforeInterruption {
                try? AVAudioSession.sharedInstance().setActive(true)
                player.play()
            }
 
        @unknown default:
            break
        }
    }
}

The crux here is how you treat shouldResume. The OS distinguishes "interruptions you may resume from" and "interruptions you should not." For example, when another music app comes to the front by the user's action, you should not grab playback back on your own. Resuming when shouldResume is not set goes against the user's intent. Check whether you were playing before the interruption as well, and only restore quietly when both hold — that is the well-behaved implementation.

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
You can subscribe to AVAudioSession interruption (calls, Siri) and route-change (headphone unplug) notifications correctly so playback never breaks
You'll separate the cases where you may resume on your own from the ones where you should wait for the user, using the shouldResume flag
You'll wire up Now Playing and remote commands to ship a production-grade audio app controllable from the lock screen and Control Center
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Rork Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

Dev Tools2026-06-24
Quietly Dialing Back Heavy Work When the Device Gets Hot or Enters Low Power Mode
How to watch ProcessInfo's thermalState and Low Power Mode and degrade heavy work in stages when the device is hot or the battery is low, with working Swift code.
Dev Tools2026-06-18
Building Apple Watch Complications with WidgetKit in a Rork Max App
A concrete walkthrough for adding watchOS complications and Smart Stack support to a SwiftUI app generated by Rork Max, covering target setup, cross-process data sharing, and timeline design.
Dev Tools2026-06-16
Adding SwiftData to a SwiftUI App Generated by Rork Max
Rork Max can produce polished SwiftUI screens, but persistence often stops at @State. Here is how I layer SwiftData onto generated code: model design, wiring the container to views, and a schema migration pattern that survives shipping.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →