Speech-to-text for React Native, built on the platform speech recognizers
(iOS SFSpeechRecognizer, Android SpeechRecognizer) as a New Architecture
TurboModule.
- React Native 0.76+ with the New Architecture enabled (the default since 0.76)
npm install voice-recognition-react-native
# or
yarn add voice-recognition-react-nativeAdd the usage descriptions to your Info.plist:
<key>NSSpeechRecognitionUsageDescription</key>
<string>We need access to speech recognition to transcribe your voice</string>
<key>NSMicrophoneUsageDescription</key>
<string>We need access to your microphone for speech recognition</string>Then install pods:
cd ios && pod installAdd the permission to android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.RECORD_AUDIO" />import { useEffect, useState } from 'react';
import { Button, SafeAreaView, Text } from 'react-native';
import {
addRecognitionListener,
startRecognition,
stopRecognition,
} from 'voice-recognition-react-native';
export default function App() {
const [transcription, setTranscription] = useState('');
useEffect(() => {
const subscription = addRecognitionListener(
(result) => setTranscription(result),
(error) => console.warn(error.code, error.message),
(partial) => setTranscription(partial) // optional: live results
);
return () => subscription.remove();
}, []);
const handleStart = async () => {
try {
const finalResult = await startRecognition({ locale: 'en-US' });
setTranscription(finalResult);
} catch (error) {
console.warn(error);
}
};
return (
<SafeAreaView>
<Button title="Start" onPress={handleStart} />
<Button title="Stop" onPress={() => stopRecognition()} />
<Text>{transcription}</Text>
</SafeAreaView>
);
}Starts speech recognition and resolves with the final transcription. On Android the microphone permission is requested automatically if needed.
| Option | Type | Default | Description |
|---|---|---|---|
locale |
string |
'en-US' |
BCP-47 language tag, e.g. 'ko-KR' |
Rejections carry a code property: permission_denied, not_available,
language_not_supported, no_match, network, speech_timeout, busy,
audio_error, server_error, recognition_error.
Stops the current session. The pending startRecognition promise resolves
with whatever was transcribed so far.
Sessions keep listening through pauses until you call stopRecognition().
On Android the platform recognizer ends after each utterance, so the library
transparently restarts it and concatenates the segments. Note that on iOS,
Apple's recognizer limits a single session to roughly one minute.
Subscribes to recognition events:
onResult(result: string)— final transcription of a sessiononError(error: { code: string; message: string })— recognition errorsonPartialResult(result: string)— live transcription while speaking (optional)
Returns a subscription; call subscription.remove() to unsubscribe. Multiple
independent subscriptions can coexist.
Requests the Android microphone permission up front (always resolves true
on iOS, where the system prompts when recognition starts). Useful if you want
to control when the permission dialog appears.
MIT
Made with create-react-native-library