[Android] Recording interruptions without audio focus (observeRecordingInterruptions / isRecordingSilenced)

Aperta
#1,388 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@JaydeepAgravat ci sta già lavorando.

Dal 21/11/2026.

  • #1488 di @JaydeepAgravat — aperta

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
15/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
android, cpp, kotlin, react-native, typescript

Direzione di ricerca

Start with the proposed RecordingSilenceListener.kt and the AudioManager.ts API surface the issue links, then find where MediaSessionManager.cleanup() and the recorder's pause() live. Do not start coding until a maintainer decides on the opt-in androidSharedInput recorder option, since the input stream's sharing mode depends on it. Done means the API is agreed, the Android 29+ listener is wired with a no-op elsewhere, and the old-arch spec, web no-op and Jest mock are updated.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

android feature-request
Feature Request
Problem

On Android, the only way to learn that a recording was interrupted (a phone call, a VoIP call, the Assistant, another recorder) is AudioManager.observeAudioInterruptions(...), which requests audio focus. Focus is a playback contract, and for a recording-only app it has two costs:

  1. Starting a recording stops the user's music. AUDIOFOCUS_GAIN makes Spotify/YouTube/a play-along app pause. Apps where people record themselves playing along to a backing track or a metronome app can't use it.
  2. Other apps' playback pauses the recording, and it never comes back. When the user starts music, the recorder gets a permanent AUDIOFOCUS_LOSS; no AUDIOFOCUS_GAIN follows while the other app keeps playing, so the recording stays paused. Telling a call (transient) from music (permanent) is exactly what #1358 asks for, but even with that distinction the app has to take focus to hear about calls at all.

Capture doesn't need focus. Since Android 10 (API 29) the system tells an app when its own recording is silenced because something else took the microphone: AudioManager.AudioRecordingCallback → AudioRecordingConfiguration.isClientSilenced(). That is precisely "the recording was interrupted", without touching playback.

Proposal

Two methods on AudioManager, Android 29+ only (no-ops elsewhere):

/**
 * Android 29+: emit the existing `interruption` event when the system silences the
 * active AudioRecorder's capture (a call, VoIP, the Assistant, another recorder, the
 * mic privacy toggle) and when it lets go. Requests no audio focus, so other apps'
 * playback is left alone. No-op on iOS, web and Android < 29.
 */
observeRecordingInterruptions(enabled: boolean): void;

/**
 * Android 29+: whether the active recorder's capture is silenced right now, or the
 * interruption that silenced it is a call that's still in progress. False elsewhere.
 */
isRecordingSilenced(): boolean;
  • Events: reuses interruption ({ type: 'began' | 'ended', shouldResume }). One began when the recorder's capture becomes silenced (including a recording that starts already silenced), one ended with shouldResume: true when it's released. No new event types; no AudioEvent enum change.
  • Matching "our" recording: the input stream gets an allocated audio session id, and the listener matches AudioRecordingConfiguration.getClientAudioSessionId() against the recorder's current stream id on every callback (a disconnect reopens the stream with a new id). A callback without our configuration is "unknown", never "unsilenced".
  • Calls: ended is held while the audio mode is a call mode (MODE_IN_CALL, MODE_IN_COMMUNICATION, MODE_CALL_SCREENING) that started after began — via OnModeChangedListener on API 31+, polling on 29–30 — so a recording isn't resumed mid-call. A communication mode the host app itself had already set (its own VoIP/SCO) doesn't count.
  • Lifetime: registered once on the main looper with counted enable/disable; released in MediaSessionManager.cleanup() on module invalidate.
  • isRecordingSilenced() lets an app refuse its own resume while the mic is taken (otherwise a resume during a call records silence).
Input stream configuration (needs a maintainer decision)

For the system to silence (and later un-silence) our capture rather than disconnect it, the input stream has to stay off MMAP. Today the recorder opens SharingMode::Exclusive (with PerformanceMode::None); an exclusive/MMAP input stream tends to be disconnected when the policy reroutes. We'd propose an opt-in recorder option (e.g. androidSharedInput: true, name open) rather than changing the default, since it can raise input latency for apps that want it low. Two related points from our implementation, happy to split them out:

  • AAudio can't pause an input stream (pause() → ErrorUnimplemented), so the stream keeps running while paused and the data callback drops buffers. A disconnect reclaim must therefore restart the stream even while paused — a stopped stream leaves the active recording configurations, so the end of the interruption is never reported. (Upstream's reclaim was restructured into restartCapture(); we'd check this there.)
  • A related bug fix, separate PR: a pause requested while the stream is reconnecting is dropped today (pause() returns early on !isStreamRecording()), so the recorder keeps writing after the reconnect.
Evidence

Device spike (Pixel 3 XL, Android 12, and an API 34 emulator):

Case Silenced vs disconnected began ended
Signal (VoIP) call, Pixel silenced, no disconnect when the other app opened the mic within ~10 ms of hang-up
Cellular call, emulator disconnected 1–2× at ring, then silenced at ring at hang-up (once the reclaim restarts the stream while paused)

With the app pausing on began (buffers dropped while paused) and resuming on ended, the saved file excluded the call: 1165.7 s recorded vs 1166.0 s expected for a ~20 min recording with a ~28 s call. Across 12 answered emulator calls the recorded duration never advanced during a call.

Compatibility / scope
  • Additive and opt-in; observeAudioInterruptions (focus) is unchanged and remains the right choice for playback apps.
  • iOS: no-op — iOS already reports interruptions through the audio session; a mixable playAndRecord session gets the same "only mic-takers interrupt" behaviour.
  • Web: no-op. Old-arch spec, web spec and Jest mock updated.
  • Android < 29: no-op (no isClientSilenced); apps keep using focus there.
Alternatives considered
  • Transient vs permanent focus loss (#1358's suggestion): still requires taking focus (so starting a recording stops music), and once focus is lost permanently the app stops hearing later calls.
  • Audio-mode / telephony listening only: misses the Assistant, other recorders and the mic privacy toggle; we use the audio mode only to hold ended during a call.
Implementation

We run this in production-bound code on our fork (based on 0.13.6): RecordingSilenceListener.kt, the AudioManager API and docs. If the API shape works for you, we'll port it onto main as a PR (including the old-arch spec, web no-op, mock and docs). Related: #1358.

Lingua principale
C++
Stelle
864
Fork
103
Merge medio
1g 16h
PR unite (30g)
77

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di software-mansion/react-native-audio-api

Tutte le issue di software-mansion/react-native-audio-api

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.