Skip to content

quannh287/react-native-live-audio-stream

 
 

Repository files navigation

react-native-live-audio-record

npm

Get live audio stream data for React Native with Android background recording support. Ideal for live voice recognition, transcribing, and continuous audio monitoring.

📖 About

This library is forked from xiqi/react-native-live-audio-stream and enhanced with background audio recording capabilities.

What's New:

  • Android: Foreground service with wake lock and battery optimization awareness

Install

yarn add react-native-live-audio-record
cd ios
pod install

Permissions and Setup

iOS

Add these lines to ios/[YOUR_APP_NAME]/info.plist

<key>NSMicrophoneUsageDescription</key>
<string>This app needs access to microphone to record audio for streaming.</string>

<!-- Background modes for audio recording -->
<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
    <string>background-processing</string>
</array>

<!-- Required device capabilities -->
<key>UIRequiredDeviceCapabilities</key>
<array>
    <string>microphone</string>
</array>

Android

Add the following permissions to android/app/src/main/AndroidManifest.xml

<!-- Required permissions -->
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<!-- Service declaration -->
<application>
    <service
        android:name="com.imxiqi.rnliveaudiostream.RNLiveAudioStreamService"
        android:enabled="true"
        android:exported="false"
        android:foregroundServiceType="microphone" />
</application>

Background Recording Support

This library now supports background audio recording on both platforms:

  • Android: Uses foreground service with persistent notification

Important: For reliable background recording, users should:

  1. iOS: Enable "Background App Refresh" for your app
  2. Android: Disable battery optimization for your app in device settings

Usage

import LiveAudioStream from 'react-native-live-audio-record';
// yarn add buffer
import { Buffer } from 'buffer';

const options = {
  sampleRate: 32000,  // default is 44100 but 32000 is adequate for accurate voice recognition
  channels: 1,        // 1 or 2, default 1
  bitsPerSample: 16,  // 8 or 16, default 16
  audioSource: 6,     // android only (see below)
  bufferSize: 4096    // default is 2048
};

LiveAudioStream.init(options);
LiveAudioStream.on('data', data => {
  // base64-encoded audio data chunks
  var chunk = Buffer.from(data, 'base64');
});
  ...
LiveAudioStream.start();
  ...
LiveAudioStream.stop();
  ...

audioSource should be one of the constant values from here. Default value is 6 (VOICE_RECOGNITION).

Contributing

Feel free to submit issues and pull requests. For major changes, please open an issue first to discuss what you would like to change.

Development Setup

git clone <your-fork>
cd react-native-live-audio-record
npm install

# Test on example project
cd example
npm install
npx react-native run-ios
npx react-native run-android

Credits/References

Original Library:

Background Recording Enhancements:

Additional References:

License

MIT

Support

About

Get live audio stream data for React Native (works for iOS and Android)

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Java 79.6%
  • Objective-C 14.2%
  • JavaScript 4.3%
  • Ruby 1.9%