Skip to content

PlatformAudio: the music comes back after the app returns, and follows only the game's music switch - #224

Merged
fleon merged 3 commits into
mainfrom
fix/music-resumes-on-foreground
Nov 28, 2026
Merged

fleon merged 3 commits into
mainfrom
fix/music-resumes-on-foreground

Conversation

@fleon

@fleon fleon commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Players reported that the background music stops when they leave the game and doesn't come back. This makes it come back on return to the foreground, after a plain app switch and after an interruption, whenever the game's music switch is on. It also removes the feature that silenced the soundtrack while another app played audio (stepsAsideForOtherAudio), which misjudged whether other audio was playing.

Only the game's music switch silences its music now, on both platforms. The soundtrack mixes with other apps' audio. The player's own music plays on under it and is never paused by the game, and the game never steps aside or pauses for other audio or calls.

Root cause

  • Stepping aside never undid itself. On iOS the soundtrack went quiet while secondaryAudioShouldBeSilencedHint was true, and the hint was read only at launch and through its notification, which iOS sends only to apps in the foreground. After the player used another app with audio, the game came back still thinking audio was playing, and stayed silent. The hint is also true while another app merely holds a non-mixable session. On Android the game read isMusicActive() as it returned and took no focus if anything was playing. It never asked again, and on Android 12+ an app that lost focus stays muted until it asks again.
  • Restarting at the wrong moment. iOS restarted the engine on willEnterForeground, while the app is still in the background state. Nothing restarted it on didBecomeActive, the one moment that comes after an interruption that never says it ended (Siri, a declined call).

What changed

iOS / tvOS / Mac Catalyst (AppleAudioBackend)

  • Session: .ambient (the explicit .mixWithOthers was redundant and is gone). I picked it because it's the one category where other apps can't silence the game's music. It mixes with other apps' audio: starting Music or a video doesn't interrupt the game's music, and the game never stops the player's. It honours the Ring/Silent switch, as games are expected to. .soloAmbient would stop the player's music on every launch, and other apps' playback would interrupt the game. .playback would ignore the silent switch.
  • iOS itself silences every app for a call, Siri or an alarm, and no app can opt out. The soundtrack comes back as that ends, or at the latest as the app is next active.
  • Restarts on didBecomeActive, with the session reactivated first (off the main thread). An interruption's end also reactivates the session before the restart.
  • Ignores interruption-began notifications that iOS delivers late for an app that was suspended (.appWasSuspended / WasSuspendedKey): pausing for one would wait for an end that never comes.
  • Drops onOtherAudioChange, isOtherAudioPlaying and the silence-hint observer.

Android (DeviceServices)

  • The game holds no audio focus, the counterpart of iOS's ambient session. Its soundtrack mixes with whatever else plays, and the player's own music keeps playing.
  • Holding no focus, nothing can silence it: not another app's playback, not Android 12+'s fade-out, and not a call (Android 12+ mutes only focus holders during a call).
  • The shell still pauses all audio while the game is in the background, as before, so nothing plays over the launcher.
  • AudioFocusPolicy, the focus requests and the audio event are gone.

API (breaking; each game fixes it when it next moves its submodule)

  • Removed:
    • AudioConfiguration.stepsAsideForOtherAudio
    • AudioManager.audioFocusDidChange(otherAudio:interrupted:)
    • AndroidPlatformServices.Configuration.audioFocusChanged
    • .toolkit's audio: parameter
  • A game on .toolkit(gameCenter:…) with the default AudioConfiguration needs no changes, as is the case for Sheeping Around's main. A game that passes stepsAsideForOtherAudio:, wires audioFocusChanged by hand or passes audio: to .toolkit drops that line.

AGENTS.md (the rule that kept the steps-aside default), README and the template README now describe the new behaviour.

Verification

  • swift test --package-path Platform passes. Platform's tests pass on Mac Catalyst. Scripts/check-android.sh arm64-v8a passes. The Android library's unit tests and assembleDebug pass.
  • The first version also passed swift test and the iOS and tvOS Platform builds. The later commits change only AudioManager, which those builds compile as before, and remove the Android focus code. CI covers them again.
  • iPhone 16 Pro. Sheeping Around at its latest main, built against this branch (iOS side unchanged since), with a temporary tap on the main mixer that measured rendered frames and peak level:
    • Switching to Settings and back (8 s, 40 s, 5 min): the engine now restarts once the app is active, and music renders again right after every return (peak about 0.6).
    • With Safari playing an mp3 while the game was in the background, the game returned with the silence hint true, and its music still rendered. Under the old default the game would have stayed silent.
  • Android: not checked on a device. The Galaxy M31 was offline. On a pool emulator, before this last change, the game's AAudio stream was running in front, paused in the background and running again after it came back. With no focus at all, nothing remains that could keep it quiet.

@fleon
fleon marked this pull request as ready for review November 28, 2026 11:17
@fleon
fleon enabled auto-merge November 28, 2026 11:19
@claude

claude Bot commented Nov 28, 2026

Copy link
Copy Markdown

Claude review (house rules)
Reviewed 14 files; read AGENTS.md (CONTRIBUTING.md isn't in the repo).
Left 1 inline comment.

@claude

claude Bot commented Nov 28, 2026

Copy link
Copy Markdown

Claude review (house rules), correction: CONTRIBUTING.md is in the repo. I read it after posting the comment above, and it doesn't change the review. Still 1 inline comment.

@fleon
fleon disabled auto-merge November 28, 2026 11:19
…s only the game's music switch

The soundtrack no longer steps aside for other apps' audio
(`stepsAsideForOtherAudio`, iOS's secondary-audio hint, Android's
isMusicActive check), which misjudged what was playing and, once it
stepped aside, often never came back.

iOS: an ambient session (mixes with other apps, never interrupted by
their playback, silenced by the Ring/Silent switch). The soundtrack picks
up on didBecomeActive with the session reactivated, rather than on
willEnterForeground while the app is still in the background, so it also
comes back after an interruption that never says it ended (Siri, a
declined call). Interruptions told late for a suspended app are ignored.

Android: the game takes audio focus each time it comes back from a stop
while its music is on (Android keeps an app that lost focus muted until it
asks again), and holds none while music is off. Focus asked during a call
is granted as it ends. AndroidPlatformServices.Audio replaces
audioFocusChanged; AudioManager.audioFocusDidChange(interrupted:)
replaces audioFocusDidChange(otherAudio:interrupted:).
… under the game's own key

Services install before the game configures its audio, so the switch the
shell first heard was read under the default key: configure posts
musicEnabledDidChange too (the policy ignores a repeat).
@fleon
fleon force-pushed the fix/music-resumes-on-foreground branch from 027d41a to 0bb39dd Compare November 28, 2026 11:21
…sic switch silences the soundtrack

The game holds no audio focus, so its soundtrack mixes with the player's
own music, which plays on, and nothing (another app's playback, Android
12's fade-out, a call) silences it. Calls don't pause it either: only the
game's music switch does.

Gone with focus: AudioFocusPolicy, the device.audioFocus call and the
audio event, AndroidPlatformServices.Configuration.audioFocusChanged,
.toolkit's audio parameter, AudioManager.audioFocusDidChange and
musicEnabledDidChange.
@fleon
fleon enabled auto-merge November 28, 2026 11:27
@fleon
fleon merged commit ba5f558 into main Nov 28, 2026
14 checks passed
@fleon
fleon deleted the fix/music-resumes-on-foreground branch November 28, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant