diff --git a/.gitignore b/.gitignore index 16871cc8..f63fe0df 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ local.properties # Visual Studio Code .vscode/ +.gradle diff --git a/README.md b/README.md index 1667dc1d..7547e838 100644 --- a/README.md +++ b/README.md @@ -1,263 +1,226 @@ -![CircleCI branch][circle-ci-badge] -[![npm][npm]][npm-url] +# React Native Voice -

React Native Voice

-

A speech-to-text library for React Native.

+[![npm](https://img.shields.io/npm/v/@react-native-voice/voice.svg?style=flat-square)](https://npmjs.com/package/@react-native-voice/voice) - -chat on Discord +🎤 React Native Voice Recognition library for iOS and Android (Online and Offline Support) + +## Features + +- ✅ **New Architecture Support** - Works with Fabric and TurboModules +- ✅ **Bridgeless Mode** - Full support for React Native's Bridgeless mode +- ✅ **React Native 0.76+** - Tested and working with the latest RN versions +- ✅ **Cross-platform** - Works on both iOS and Android +- ✅ **Online and Offline** - Supports both online and offline speech recognition + +## Installation ```sh yarn add @react-native-voice/voice # or -npm i @react-native-voice/voice --save +npm install @react-native-voice/voice --save ``` -Link the iOS package +### iOS Setup ```sh -npx pod-install +cd ios && pod install ``` -## Table of contents +### Android Setup -- [Linking](#linking) - - [Manually Link Android](#manually-link-android) - - [Manually Link iOS](#manually-link-ios) -- [Prebuild Plugin](#prebuild-plugin) -- [Usage](#usage) - - [Example](#example) -- [API](#api) -- [Events](#events) -- [Permissions](#permissions) - - [Android](#android) - - [iOS](#ios) -- [Contributors](#contributors) +No additional setup required - autolinking handles everything. -

Linking

+## Usage -

Manually or automatically link the NativeModule

+```javascript +import Voice from '@react-native-voice/voice'; -```sh -react-native link @react-native-voice/voice -``` +// Set up event handlers +Voice.onSpeechStart = () => console.log('Speech started'); +Voice.onSpeechEnd = () => console.log('Speech ended'); +Voice.onSpeechResults = (e) => console.log('Results:', e.value); +Voice.onSpeechPartialResults = (e) => console.log('Partial:', e.value); +Voice.onSpeechError = (e) => console.log('Error:', e.error); -### Manually Link Android +// Start listening +await Voice.start('en-US'); -- In `android/setting.gradle` +// Stop listening +await Voice.stop(); -```gradle -... -include ':@react-native-voice_voice', ':app' -project(':@react-native-voice_voice').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-voice/voice/android') +// Clean up +await Voice.destroy(); ``` -- In `android/app/build.gradle` +### Full Example -```gradle -... -dependencies { - ... - compile project(':@react-native-voice_voice') -} -``` +```javascript +import React, { useEffect, useState, useCallback } from 'react'; +import { View, Text, Button } from 'react-native'; +import Voice from '@react-native-voice/voice'; -- In `MainApplication.java` - -```java - -import android.app.Application; -import com.facebook.react.ReactApplication; -import com.facebook.react.ReactPackage; -... -import com.wenkesj.voice.VoicePackage; // <------ Add this! -... - -public class MainActivity extends Activity implements ReactApplication { -... - @Override - protected List getPackages() { - return Arrays.asList( - new MainReactPackage(), - new VoicePackage() // <------ Add this! - ); +function SpeechToText() { + const [results, setResults] = useState([]); + const [isListening, setIsListening] = useState(false); + + useEffect(() => { + Voice.onSpeechStart = () => setIsListening(true); + Voice.onSpeechEnd = () => setIsListening(false); + Voice.onSpeechResults = (e) => setResults(e.value ?? []); + Voice.onSpeechError = (e) => console.error(e.error); + + return () => { + Voice.destroy().then(Voice.removeAllListeners); + }; + }, []); + + const startListening = async () => { + try { + await Voice.start('en-US'); + } catch (e) { + console.error(e); } + }; + + const stopListening = async () => { + try { + await Voice.stop(); + } catch (e) { + console.error(e); + } + }; + + return ( + + {isListening ? '🎤 Listening...' : 'Press Start'} + {results.join(' ')} +