Speech-to-Text API for React Native: Offline Recording Guide
Learn to integrate Speech-to-Text APIs in React Native. This guide covers offline audio recording, cloud transcription, and optimizing Vietnamese accuracy.
In this article
- Why Integrate Speech-to-Text for React Native?
- System Architecture for Recording and Transcription
- Audio Format Considerations
- Step-by-Step Guide to Integrating the Speech-to-Text API
- Step 1: Prepare Audio Data
- Step 2: Send Data to the API
- Step 3: Process Results and Display
- Comparing Offline and Online Solutions
- Optimizing User Experience
- Conclusion
Integrating speech recognition into mobile applications is becoming essential for optimizing user experience. However, a significant challenge for React Native developers is how to convert voice to text smoothly, especially when the device lacks an internet connection. This article provides a detailed solution for using a Speech-to-Text API combined with a local recording module, helping you build a mobile voice recording feature that operates reliably even in offline environments.
Why Integrate Speech-to-Text for React Native?
In applications such as note-taking, translation, or virtual assistants, converting audio to text allows users to input data faster than using a virtual keyboard. For the Vietnamese market specifically, accuracy in recognizing tones and diacritical marks is a critical factor.
Many developers face difficulties when choosing a solution. On one hand, there are cloud APIs that require a constant internet connection. On the other, there are open-source libraries that run locally but often lack accuracy for Vietnamese. The optimal solution is to combine two flows: local recording (offline) to ensure availability, and a powerful cloud API when network is available to achieve the highest accuracy.
System Architecture for Recording and Transcription
To implement this feature, we need to separate the logic into two main parts:
- Audio Capture: Use popular libraries in the React Native ecosystem like
react-native-audio-recorder-playerorreact-native-mic. The role of this part is to capture raw audio data or files (MP3, WAV, OGG) and store them locally on the device. - Processing (Speech-to-Text Engine): This is where the "magic" happens. Instead of embedding a heavy model into the app, which significantly increases app size, we send the audio file or data stream to the API provider's server.
Audio Format Considerations
When recording mobile voice, the file format directly affects accuracy and processing speed.
- WAV/PCM: Best quality, larger file size, suitable for short recordings under one minute.
- MP3/OGG: Smaller size, good compression, suitable for long meetings or notes.
Recommendation: Use MP3 or OGG format with a bitrate of 128kbps or higher to balance quality and bandwidth.
Step-by-Step Guide to Integrating the Speech-to-Text API
Below is the technical process for integrating a speech recognition API into your React Native application.
Step 1: Prepare Audio Data
Before calling the API, ensure the audio file has been fully captured. If you want to support offline mobile voice recording, you should save the file to the application's temporary directory.
// Example logic for recording and saving file
const audioRecorder = new AudioRecorder();
// Configure recording parameters
await audioRecorder.configure({
audioEncoderIOS: AudioEncoderIOSSample.AAC,
audioEncoderAndroid: AudioEncoderAndroid.AAC,
sampleRate: 44100,
numberOfChannels: 1,
bitRate: 128000
});
await audioRecorder.start();
// ... User presses stop ...
await audioRecorder.stop();
const uri = await audioRecorder.getURI();
Step 2: Send Data to the API
Once you have the audio file, use fetch or axios to send a POST request to the provider's endpoint. For applications requiring high accuracy in Vietnamese, choosing the right API is crucial.
AIVISION provides a Speech-to-Text service with a focus on Vietnamese, supporting code-switching (mixing English) and providing timestamps for each word. This is extremely useful for applications that need to synchronize text with audio.
const transcribeAudio = async (audioUri) => {
const formData = new FormData();
// Add audio file to FormData
formData.append('file', {
uri: audioUri,
name: 'recording.mp3',
type: 'audio/mpeg'
});
const response = await fetch('https://api.s2speech.com/v1/audio/transcriptions', {
method: 'POST',
headers: {
'Authorization': `Bearer YOUR_API_KEY`
},
body: formData
});
const data = await response.json();
return data.text;
};
Step 3: Process Results and Display
After receiving the text from the API, you need to handle error cases and display the results on the interface. If you are building a voice-to-text app feature for meetings, leverage the word timestamps feature to create dynamic highlights when users scrub back through the audio.
Comparing Offline and Online Solutions
The decision to run STT entirely offline or online depends on the specific product requirements. The table below summarizes the pros and cons:
| Criteria | Offline Solution (On-device) | Online Solution (Cloud API) |
|---|---|---|
| Network Connection | Not required | Required |
| App Size | Large (requires embedded model) | Small (API calls only) |
| Accuracy | Average for Vietnamese | High, continuously updated |
| Speed | Depends on device chip | Depends on bandwidth |
| Security | Data stays on device | Data passes through server |
Advice: If your application requires high security or must operate in environments without network access (such as airplanes or mines), consider a hybrid solution. Record offline, and automatically sync to the API when a network is available.
Optimizing User Experience
To make the mobile voice recording feature truly useful, you need to pay attention to UX/UI elements:
- Visual Feedback: Display a microphone icon and a sine wave animation so users know the app is listening.
- Noise Handling: If possible, apply basic noise filtering before sending the file to the API to increase accuracy.
- History Storage: Allow users to review previously transcribed records.
AIVISION has integrated audio pre-processing algorithms that help reduce background noise, which is particularly useful in office or street environments. With a training dataset of hundreds of thousands of hours of Vietnamese speech, AIVISION's models are optimized to understand Vietnamese context clearly, thereby minimizing recognition errors.
Conclusion
Integrating a Speech-to-Text API into React Native is a strategic step to enhance the value of your mobile application. By combining a local recording module with a high-quality cloud API, you can provide a smooth, accurate voice-to-text experience with strong support for Vietnamese.
Don't let technical barriers or low accuracy slow down your product's progress. Start by testing the API to evaluate real-world speech recognition quality.
You can Start free to try AIVISION's Speech-to-Text services today and verify the accuracy on your own data. If you need deeper technical consultation on integration, please Contact our development team. Explore more technology insights on our Blog.
Frequently asked questions
Does the Speech-to-Text API for React Native support Vietnamese?
Yes, AIVISION Speech-to-Text is designed with a focus on Vietnamese, supporting accurate recognition of tones and specialized terms, while also handling English code-switching effectively.
Can I record and transcribe audio when there is no internet connection?
You can record audio completely offline. However, the transcription step (STT) typically requires a network connection to send data to the server for processing. You can save the audio file locally and automatically sync it when a network becomes available.
Which audio format is suitable for mobile voice recording?
MP3 and OGG are recommended formats due to their small size and good quality. WAV can also be used for short segments if absolute accuracy is needed and file size is not a concern.
How can I improve the accuracy of voice-to-text conversion in my app?
You should ensure good recording quality and minimize background noise. Using an API that supports word timestamps and is trained on a large Vietnamese dataset, such as AIVISION's, also helps significantly increase accuracy.
What is the cost of using the Speech-to-Text API?
AIVISION offers a free trial for every account, with no credit card required. Each account has a daily usage limit that is pre-set and can be adjusted according to your needs.
Try AIVISION's Vietnamese speech AI
Speech-to-Text, Text-to-Speech and LLM, free.
Start free → Contact