Skip to content

feat: stop background audio during recording, resume after#317

Open
adithya1012 wants to merge 2 commits intomieweb:mainfrom
adithya1012:interruptionMode_mode
Open

feat: stop background audio during recording, resume after#317
adithya1012 wants to merge 2 commits intomieweb:mainfrom
adithya1012:interruptionMode_mode

Conversation

@adithya1012
Copy link
Copy Markdown
Collaborator

@adithya1012 adithya1012 commented Mar 12, 2026

Description

Pulse now will handle the background app playing while recording the audio.
Added a local native Expo module (audio-focus) that directly controls the OS-level audio focus on both Android and iOS. A useAudioSession hook integrates it into the recording flow.


Demo

LINK


How it works

Android

  • requestAudioFocusAudioManager.requestAudioFocus(AUDIOFOCUS_GAIN_TRANSIENT) — OS signal that causes Spotify/podcasts to pause
  • abandonAudioFocusAudioManager.abandonAudioFocus() — tells Spotify/podcasts they can resume

AUDIOFOCUS_GAIN_TRANSIENT (not AUDIOFOCUS_GAIN) is intentional — transient means Spotify receives AUDIOFOCUS_LOSS_TRANSIENT and automatically resumes when we're done.

Why not expo-audio's setIsAudioActiveAsync?
It is a complete no-op on Android — it only sets an internal flag and never calls requestAudioFocus. Confirmed in AudioModule.kt lines 183–195.

iOS

  • requestAudioFocusAVAudioSession.setActive(true) — activates session, interrupting other apps
  • abandonAudioFocusAVAudioSession.setActive(false, options: .notifyOthersOnDeactivation) — the .notifyOthersOnDeactivation flag is the iOS signal that tells Spotify/Apple Music to resume. Without this flag, they stay paused indefinitely.

setAudioModeAsync (expo-audio) configures the AVAudioSession category (playAndRecord, doNotMix) on both platforms before focus is claimed.


Files changed

File Change
audio-focus New local native Expo module (Kotlin + Swift)
useAudioSession.ts New hook — activateRecordingSession / deactivateRecordingSession
shorts.tsx Calls activate on record tap, deactivate on stop and screen unmount
AndroidManifest.xml Adds MODIFY_AUDIO_SETTINGS permission
.gitignore Excludes audio-focus Android build output

Bug fixed during implementation

expo/expo#34025 — passing both interruptionMode (iOS) and interruptionModeAndroid in the same setAudioModeAsync call crashes on Android with a Kotlin enum cast error. Fixed by spreading only the platform-relevant key via Platform.OS.


Testing

  1. Start playing music in Spotify
  2. Open the camera screen → music pauses immediately
  3. Record a clip → silence throughout
  4. Stop recording → Spotify resumes automatically ✅

NOTE: Tested in Android. This need to be tested in iOS.

@adithya1012 adithya1012 marked this pull request as ready for review March 12, 2026 02:01
@adithya1012 adithya1012 requested a review from morepriyam March 12, 2026 23:13
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.

2 participants