react-native-live-audio
v0.1.2
Published
Live microphone buffering with pre-roll/post-roll WAV capture for React Native (Nitro, iOS).
Maintainers
Readme
react-native-live-audio
Live microphone buffering for React Native with pre-roll and post-roll capture to a WAV file on disk. Implemented as a Nitro module (native Swift on Apple platforms).
Features
- Continuous tap on the microphone input while
start()is active, with a configurable rolling history buffer. capture()exports a window that includes audio before and after the call (pre-roll / post-roll).- Optional gain applied only at export time (live buffer stays unprocessed).
- Typed JavaScript API and autolinking via
react-native-nitro-modules.
Platform support
| Platform | Support | | -------- | ------- | | iOS | Yes | | visionOS | Yes (via CocoaPods target) | | Android | Not supported by this library (no native module). |
Requirements
- React Native
>= 0.76(tested with0.84.x). - react-native-nitro-modules compatible with your Nitro codegen (this repo is developed against
0.35.x). - Node.js
>= 18. - iOS: Xcode and CocoaPods; microphone usage string in
Info.plist(see below).
Installation
npm install react-native-live-audio react-native-nitro-modulesThen install iOS pods from your app’s ios directory:
cd ios && bundle exec pod installRebuild the native app after adding the dependency.
iOS configuration
Add a microphone usage description to your app’s Info.plist:
<key>NSMicrophoneUsageDescription</key>
<string>Explain why your app needs the microphone.</string>Without this key, iOS will terminate or block microphone access when you call LiveAudio.start().
Quick start
import { LiveAudio } from 'react-native-live-audio';
// Begin buffering (default max pre-roll history: 10 seconds)
LiveAudio.start({ maxPreRollSeconds: 10 });
// Later: export ~1.5s before + ~1.5s after this moment to a temp WAV path
const path = await LiveAudio.capture({
preRollSeconds: 1.5,
postRollSeconds: 1.5,
gain: 5,
});
LiveAudio.stop();If capture() fails or there is no usable audio, the promise resolves to an empty string (check before uploading or playing the file).
API
LiveAudio.start(options?)
Starts the audio engine and ring buffer.
options.maxPreRollSeconds(optional, default10): Upper bound on how much history is kept for pre-roll. Must be between0and60. Increase this if you callcapture()with a largerpreRollSecondsthan the default.
LiveAudio.stop()
Stops the engine, clears the buffer, and cancels any in-flight capture.
LiveAudio.hello(): string
Returns a short native status string (for example idle, recording, or capturing). Useful for debugging.
LiveAudio.capture(options?): Promise<string>
Exports a WAV file while start() has the engine running.
preRollSeconds(default1.5): Audio taken from the ring buffer before the capture instant. Must be between0and the currentmaxPreRollSecondsused instart().postRollSeconds(default1.5): Audio recorded after the call until this duration elapses. Must be between0and30.gain(default5): Linear gain applied only in the exported file. Must be between0.01and50.
Resolves with an absolute file path to a file under the app’s temporary directory (for example …/tmp/capture_<UUID>.wav). Only one capture may run at a time.
Constants
DEFAULT_MAX_PRE_ROLL_SECONDS— default10.DEFAULT_CAPTURE_OPTIONS— defaultpreRollSeconds,postRollSeconds, andgainused when options are omitted.
Example app
This repository includes an example app under example/. See example/README.md for how to run it.
Troubleshooting
LiveAudio.start() must be called before capture()— Callstart()and wait until the engine is running beforecapture().- Empty path after
capture()— Often no chunks in the pre/post window, export error, or missing microphone permission. ConfirmNSMicrophoneUsageDescriptionand system microphone permission for the app. - Android — This package does not ship an Android implementation; importing it on Android will not provide native functionality.
Contributing
Issues and pull requests are welcome on GitHub. For larger changes, please open an issue first to agree on direction.
Publishing (maintainers)
- From the repository root,
npm publishrunsprepublishOnly, which executesnpm run buildso thelib/output is always included in the tarball (see thefilesfield inpackage.json). - This repo is wired for semantic-release via
npm run release(seerelease.config.cjsand.github/workflows/release.yml). Use conventional commits so versions andCHANGELOG.mdstay accurate.
License
MIT. See LICENSE.
Credits
Scaffolded with create-nitro-module.
