React Native Voice

repository·master·Indexed 24 days ago

https://github.com/react-native-voice/voice

A speech-to-text library for React Native applications on iOS and Android. Provides a Voice API to start, stop, and cancel speech recognition, and handle events such as onSpeechResults and onSpeechError. Note: This package is currently deprecated and archived; expo-speech-recognition is recommended as an actively maintained alternative.

Tokens
4.2K
Snippets
12
Records
26
Agent score
79%

What's inside @react-native-voice/voice

  1. Configure the Expo Prebuild Plugin

    master

    This package requires custom native code and cannot be used in the standard 'Expo Go' app. To use it with Expo, you must use a development build.

    Add the @react-native-voice/voice config plugin to the plugins array in your app.json or app.config.js:

    {
      "expo": {
        "plugins": ["@react-native-voice/voice"]
      }
    }

    After adding the plugin, you must rebuild your native app.

    {
      "expo": {
        "plugins": ["@react-native-voice/voice"]
      }
    }
  2. Manually link @react-native-voice/voice on iOS

    master

    To manually link the library on iOS:

    1. Drag the Voice.xcodeproj from the @react-native-voice/voice/ios folder into the Libraries group in your Xcode project.
    2. Select your main project file in Xcode, go to the Build Phases tab, and drag the static library lib.Voice.a (found in Libraries/Voice.xcodeproj/Products) into the Link Binary With Libraries section.
  3. Configure Permissions for Android

    master

    Speech recognition requires audio permissions.

    Runtime Permissions

    Since Android M (6.0), users must grant permissions at runtime. By default, calling Voice.start() will trigger the RECORD_AUDIO permission popup.

    Ejected Expo Apps

    If using an ejected Expo app, you may encounter a casting error when starting recognition. To resolve this, use expo-permission to request AUDIO_RECORDING before calling Voice.start():

    import { Permissions } from "expo";
    
    async componentDidMount() {
      const { status } = await Permissions.askAsync(Permissions.AUDIO_RECORDING);
      if (status !== "granted") {
        // Handle permission denied
        this.setState({showRecordButton: false});
      } else {
        this.setState({showRecordButton: true});
      }
    }

    Google Speech Engine

    On Android, ensure the device has a speech recognition engine installed (e.g., com.google.android.googlequicksearchbox). Users can install the Google Search App if it is missing.

    import { Permissions } from "expo";
    
    async componentDidMount() {
      const { status } = await Permissions.askAsync(Permissions.AUDIO_RECORDING);
      if (status !== "granted") {
        // Handle permission denied
        this.setState({showRecordButton: false});
      } else {
        this.setState({showRecordButton: true});
      }
    }
  4. Configure Permissions for iOS

    master

    iOS requires specific usage descriptions in your Info.plist for both the microphone and speech recognition. Add the following keys to your Info.plist:

    <dict>
      <key>NSMicrophoneUsageDescription</key>
      <string>Description of why you require the use of the microphone</string>
      <key>NSSpeechRecognitionUsageDescription</key>
      <string>Description of why you require the use of the speech recognition</string>
    </dict>
    <dict>
      <key>NSMicrophoneUsageDescription</key>
      <string>Description of why you require the use of the microphone</string>
      <key>NSSpeechRecognitionUsageDescription</key>
      <string>Description of why you require the use of the speech recognition</string>
    </dict>
  5. Manually link @react-native-voice/voice on Android

    master

    If automatic linking fails, follow these steps to manually link the NativeModule in your Android project:

    1. In android/settings.gradle: Add the following lines to include the project:

      include ':@react-native-voice_voice', ':app'
      project(':@react-native-voice_voice').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-voice/voice/android')
    2. In android/app/build.gradle: Add the project to your dependencies:

      dependencies {
          compile project(':@react-native-voice_voice')
      }
    3. In MainApplication.java: Import the package and add it to the list of packages in getPackages():

      import com.wenkesj.voice.VoicePackage; // <------ Add this!
      
      // ... inside getPackages()
      new VoicePackage() // <------ Add this!
    include ':@react-native-voice_voice', ':app'
    project(':@react-native-voice_voice').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-voice/voice/android')
  6. Reload the application to see changes

    master

    After modifying your code (e.g., in App.tsx), use the following methods to reload the app and see your changes:

    • Android: Press the <kbd>R</kbd> key twice or open the Developer Menu (<kbd>Ctrl</kbd> + <kbd>M</kbd> on Windows/Linux or <kbd>Cmd ⌘</kbd> + <kbd>M</kbd> on macOS) and select "Reload".
    • iOS: Press <kbd>Cmd ⌘</kbd> + <kbd>R</kbd> in the iOS Simulator.
  7. Configure Expo plugin props for permissions

    master

    The Expo config plugin allows you to customize the permission messages used in Info.plist for iOS.

    Available props:

    • speechRecognition: (string | false) Sets the message for NSSpeechRecognitionUsageDescription. Set to false to skip permission.
    • microphone: (string | false) Sets the message for NSMicrophoneUsageDescription. Set to false to skip permission and avoid adding android.permission.RECORD_AUDIO to the manifest.

    Example configuration:

    {
      "plugins": [
        [
          "@react-native-voice/voice",
          {
            "microphonePermission": "CUSTOM: Allow $(PRODUCT_NAME) to access the microphone",
            "speechRecognitionPermission": "CUSTOM: Allow $(PRODUCT_NAME) to securely recognize user speech"
          }
        ]
      ]
    }
    {
      "plugins": [
        [
          "@react-native-voice/voice",
          {
            "microphonePermission": "CUSTOM: Allow $(PRODUCT_NAME) to access the microphone",
            "speechRecognitionPermission": "CUSTOM: Allow $(PRODUCT_NAME) to securely recognize user speech"
          }
        ]
      ]
    }