Why use background audio
- Background and lock screen recording: Keep capturing audio when users leave the app or lock the device.
- Native platform UX: Surface a persistent Android foreground service notification and iOS system recording indicators without custom native code.
- Lifecycle controls: Start, stop, pause, and resume without releasing the background session when pausing.
- Realtime updates: Listen for recording state changes and handle completion or errors in your UI.
Prerequisites
- Enable the JavaScript Bridge in your Median app
- Grant microphone permission before or during your recording flow (see
checkPermissionandrequestPermission) - Learn the basics of async JavaScript (
async/await) and the optional callbacks used below
enableTranscription and sttLanguage. Availability can vary by locale.
Listener events: Real-time callbacks for lifecycle changes (idle, recording, paused, stopped, error).
Important considerations
- iOS shows a system-level recording indicator (orange dot with waveform animation while actively recording); it hides when recording stops.
- Android shows status text such as “audio recording in progress” or “paused” and exposes Launch App and Stop Recording actions on the notification.
- WAV works only on iOS; Android automatically uses M4A if you request WAV.
- OS interruptions (for example, calls or Siri) can pause the session (
SESSION_INTERRUPTED); handleerrorlistener state and the error reference table.
Implementation guide
Plugin setup
Enable the Background Audio plugin in your Median App Studio configuration, then rebuild your app. Once you enable it, you can callmedian.backgroundAudio.* functions from the JavaScript Bridge at runtime.
JavaScript Bridge functions
The following methods are available onmedian.backgroundAudio. Most functions return a promise; getStatus additionally supports a callback form.
Check permissions
Returns whether the user has already granted microphone permission. This method returns a promise.Request permissions
Prompts the user for microphone permission. If the user grants it, you can start recording. This method returns a promise.Start recording
Starts a background-aware recording session. Optional configuration controls format, duration, and transcription. This method returns a promise.
Return value:
Platform UX during recording
WhenstartRecording succeeds, each platform surfaces native UI automatically. You do not need additional manifest or Info.plist changes beyond enabling the plugin and declaring standard microphone usage descriptions.
Android: Foreground service notification
Android displays a persistent notification with:- App icon and name
- Status text: “audio recording in progress” or “paused”
- Two action buttons:
- Launch App: deep-links back into the app
- Stop Recording: terminates the session and launches the app
iOS: Recording indicator and audio session
iOS activatesAVAudioSession with background mode and displays a system-level recording indicator while the session is active:
- Dynamic Island (iPhone 14+) or status bar pill
- Red dot with waveform animation during active recording
- iOS hides it automatically when recording stops
Pause recording
Pauses an active recording without releasing the background session. This method returns a promise.Resume recording
Resumes a paused session. This method returns a promise.Stop recording
Stops the session and retrieves the file URI, duration, and optional transcript. This method returns a promise.Get status
Returns the current recording state and elapsed time. Pass a callback function to use the callback form. Otherwise, this method returns a promise.
Return value:
Add a listener
Registers a listener for real-time recording lifecycle updates. Returns a listener identifier that you pass toremoveListener.
Return value:
Returns a listener identifier (opaque value) that you pass to
removeListener.
Event states you may receive:
Remove a listener
Removes a previously added listener so it stops receiving further events.
Return value:
No return value.
Error handling
Best practices for error handling
ImportantUse the listener
error state and the errorCode it provides together with this reference table when presenting in-app guidance. Combine it with getStatus checks before issuing pause, resume, or stop commands to avoid NOT_RECORDING and ALREADY_RECORDING race conditions.Example implementation
The following example shows the basic recording flow.Basic recording flow
Step 1: Check permissions
Before starting a recording session, check if the user has granted microphone permissions:Step 2: Request permission (if needed)
If permissions haven’t been granted, prompt the user:Step 3: Start recording
Start a background-aware recording session with optional configuration: Minimal configuration:Sequence diagram
High-level view: permission gate → start recording → success or failure.Testing checklist
Use this checklist to ensure Background Audio is working correctly in your app:Basic functionality
-
checkPermissionandrequestPermissionbehave as expected beforestartRecording - Minimal
startRecording()and full configuration calls succeed when permitted -
pauseandresumeupdate behavior without releasing the session -
stopRecordingreturnsfileUri,durationSeconds, andtranscriptwhen you enable transcription and the platform supports it - Plugin loads without JavaScript errors
Platform testing
- Android shows the foreground notification with correct status text and Launch App / Stop Recording actions
- iOS shows the recording indicator during active recording and hides it when stopped
- WAV records on iOS as documented; Android falls back to M4A when you request WAV
- Recording continues when you background the app and lock the device
Status and events
-
getStatusreturnsstateandelapsedSecondsvia promise and viacallbackform -
addListenerreceives transitions acrossidle,recording,paused,stopped, anderror -
removeListenerstops further callbacks for that id
Error scenarios
- Your app surfaces the errors in the reference table as documented (
PERMISSION_DENIED,ALREADY_RECORDING,NOT_RECORDING,STT_UNAVAILABLE,SESSION_INTERRUPTED,FILE_ERROR) - Incoming calls or Siri trigger
SESSION_INTERRUPTED, and your listener handles it - Denying microphone permission returns
PERMISSION_DENIEDand surfaces a clear user-facing path to Settings
Troubleshooting
Handle microphone access after PERMISSION_DENIED
Guide the user to the system Settings app to enable the microphone. Confirm you call checkPermission and, if needed, requestPermission before startRecording.
Handle an active session (ALREADY_RECORDING)
Avoid duplicate starts. Use getStatus or your listener state to decide whether to start, resume, or stop instead of calling startRecording again.
Handle missing transcription (STT_UNAVAILABLE)
Speech-to-text may be unavailable for the requested locale. Fall back to the recorded file using fileUri from stopRecording, and consider setting sttLanguage explicitly to a supported locale.
Handle OS interruptions (SESSION_INTERRUPTED)
Interruptions such as phone calls or Siri can pause recording. Inform the user in your UI and let them resume when appropriate via resume.
Android notification actions behave unexpectedly
The notification includes Launch App (deep-links into the app) and Stop Recording (ends the session and launches the app). Test both actions against your app’s navigation stack.Still having trouble?For issues related to platform-level microphone, background audio, or speech-to-text behavior outside Median, refer to the platform documentation for Apple AVAudioSession and Android foreground services.