Skip to content

jiunshinn/voice-recognition-react-native

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

voice-recognition-react-native

Speech-to-text for React Native, built on the platform speech recognizers (iOS SFSpeechRecognizer, Android SpeechRecognizer) as a New Architecture TurboModule.

Requirements

  • React Native 0.76+ with the New Architecture enabled (the default since 0.76)

Installation

npm install voice-recognition-react-native
# or
yarn add voice-recognition-react-native

iOS

Add 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 install

Android

Add the permission to android/app/src/main/AndroidManifest.xml:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

Usage

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>
  );
}

API

startRecognition(options?): Promise<string>

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.

stopRecognition(): void

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.

addRecognitionListener(onResult, onError, onPartialResult?): RecognitionSubscription

Subscribes to recognition events:

  • onResult(result: string) — final transcription of a session
  • onError(error: { code: string; message: string }) — recognition errors
  • onPartialResult(result: string) — live transcription while speaking (optional)

Returns a subscription; call subscription.remove() to unsubscribe. Multiple independent subscriptions can coexist.

requestRecognitionPermission(): Promise<boolean>

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.

Contributing

License

MIT


Made with create-react-native-library

About

voice-recognition-react-native is a library that provides voice recognition functionality in React Native applications. This library supports voice recognition on iOS and Android.

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors