How xiqi/react-native-live-audio-stream works
Main Components
• JavaScript Interface: At the heart of the project is the JS API (index.js / index.d.ts) which provides functions like init, start, stop, and event listeners to receive live audio data.
• Native Bridge – Android Module: Located in android/src/main/java/... (files such as RNLiveAudioStreamModule.java and RNLiveAudioStreamPackage.java) that implement the Android native side of the module, handling the audio recording using Android’s AudioRecord API.
• Native Bridge – iOS Module: Found in the ios directory (RNLiveAudioStream.h, RNLiveAudioStream.m, and Xcode project files) where the iOS portion utilizes Audio Queues (as referenced in the README) to capture live audio.
• React Native Bridge: The communication mechanism that connects the JS code to the respective native modules on both platforms.
Component Interactions and Relationships
• The JS layer (React Native component) calls the exposed API methods (init, start, stop). These calls are bridged to the native modules based on the platform.
• On the Android side, the native module intercepts these calls (using classes in the Java directory) and interacts with Android’s low-level audio APIs.
• On the iOS side, similar functionalities are implemented to use the Audio Queues for capturing audio.
• Both native modules emit asynchronous events (audio data) that are sent back through the React Native bridge to the JS layer, which can handle these events (e.g., decoding the base64 data).
Architectural Patterns and Design Principles
• Separation of Concerns: The codebase is clearly divided between the cross-platform JS layer and the platform-specific native implementations.
• Bridging/Adapter Pattern: The native modules act as adapters between the audio capture functionalities provided by the underlying operating system and the React Native JavaScript API.
• Event-driven Architecture: The system leverages events to communicate live audio stream data from native layers to the JS layer, ensuring real-time processing.
Technologies, Frameworks, and Libraries
• React Native: The overall framework for integrating the module into a mobile app.
• Android APIs (e.g., AudioRecord) and iOS Audio Queues: Underlying native technologies to capture audio.
• Node Package (package.json): Indicates that the module is distributed as an NPM package.
• Podspec and CocoaPods (for iOS): Used for managing iOS dependencies and integration.
• Build configurations: Included in android/build.gradle and Xcode project files.