@ascendtis/react-native-voice-to-text

中文文档
0.4.2发布于 2026/10/06MITTypeScriptESM + CJS零依赖近 30 天下载 3281

This can use to convert voice to text real time in device

原始使用文档
文档目录15 节
  1. Features
  2. Requirements
  3. Installation
  4. Permissions
  5. iOS
  6. Android
  7. Usage
  8. Switching the recognition language
  9. API Reference
  10. Methods
  11. Events
  12. Troubleshooting
  13. Contributing
  14. License
  15. Credits

@ascendtis/react-native-voice-to-text

npm version npm downloads license

Convert voice to text in real time using the native speech recognition capabilities of iOS and Android.

This package is a maintained carry-forward of the unmaintained react-native-voice-to-text, rebuilt with full iOS support, New Architecture (TurboModule) compatibility, and ongoing maintenance.

Features

  • Native speech recognition on both iOS (SFSpeechRecognizer) and Android (SpeechRecognizer)
  • Real-time partial results as the user speaks
  • Final transcription with confidence scores (Android)
  • Volume level detection (Android)
  • Multi-language support: query, list, and switch the recognition language
  • New Architecture ready (TurboModule with codegen)
  • Written in TypeScript with full type definitions

Requirements

PlatformMinimum
iOS15.1
AndroidAPI 24 (Android 7.0)
React Native0.78+ (built and tested against 0.78.1)

Note: This package contains native code, so it does not work in Expo Go. Use a development build or a bare React Native project.

Installation

npm install @ascendtis/react-native-voice-to-text

or

yarn add @ascendtis/react-native-voice-to-text

The package is auto-linked on both platforms. On iOS, install the CocoaPods dependency:

cd ios && pod install

Then rebuild your app so the native module is registered.

Permissions

iOS

Add the following keys to your Info.plist:

<key>NSMicrophoneUsageDescription</key>
<string>This app needs access to your microphone for speech recognition</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>This app needs access to speech recognition to convert your voice to text</string>

Android

Add the following permission to your AndroidManifest.xml:

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

For Android 6.0+ (API 23), you also need to request the permission at runtime:

import { Platform, PermissionsAndroid } from 'react-native';

// Request microphone permission
async function requestMicrophonePermission() {
  if (Platform.OS !== 'android') return true;

  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
      {
        title: 'Microphone Permission',
        message: 'This app needs access to your microphone for speech recognition',
        buttonPositive: 'OK',
      }
    );
    return granted === PermissionsAndroid.RESULTS.GRANTED;
  } catch (err) {
    console.warn(err);
    return false;
  }
}

Usage

import React, { useEffect, useState } from 'react';
import { Button, Text, View } from 'react-native';
import {
  addEventListener,
  destroy,
  startListening,
  stopListening,
} from '@ascendtis/react-native-voice-to-text';

export default function SpeechToText() {
  const [result, setResult] = useState('');
  const [isListening, setIsListening] = useState(false);

  useEffect(() => {
    const subscriptions = [
      addEventListener('onSpeechStart', () => setIsListening(true)),
      addEventListener('onSpeechEnd', () => setIsListening(false)),
      addEventListener('onSpeechResults', (event) => {
        setResult(event.value ?? '');
      }),
      addEventListener('onSpeechError', (event) => {
        console.error('Speech recognition error:', event.message);
        setIsListening(false);
      }),
    ];

    return () => {
      subscriptions.forEach((subscription) => subscription.remove());
      destroy();
    };
  }, []);

  const toggleListening = async () => {
    try {
      if (isListening) {
        await stopListening();
      } else {
        setResult('');
        await startListening();
      }
    } catch (error) {
      console.error(error);
    }
  };

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>{result || 'Say something...'}</Text>
      <Button
        title={isListening ? 'Stop Listening' : 'Start Listening'}
        onPress={toggleListening}
      />
    </View>
  );
}

Switching the recognition language

import {
  getSupportedLanguages,
  setRecognitionLanguage,
} from '@ascendtis/react-native-voice-to-text';

const languages = await getSupportedLanguages(); // e.g. ['en-US', 'en-GB', 'fr-FR', ...]
const success = await setRecognitionLanguage('en-GB');

API Reference

All functions are named exports of the package. If the native module is not available (e.g. the app was not rebuilt after installation, or pod install was skipped), every function throws a descriptive error explaining how to fix the setup.

Methods

MethodDescriptionReturn Type
startListening()Start speech recognitionPromise<string>
stopListening()Stop speech recognitionPromise<string>
destroy()Clean up speech recognition resourcesPromise<string>
getRecognitionLanguage()Get current recognition languagePromise<string>
setRecognitionLanguage(languageTag)Set recognition language (e.g. 'en-US')Promise<boolean>
isRecognitionAvailable()Check if speech recognition is available on the devicePromise<boolean>
getSupportedLanguages()Get list of supported language codesPromise<string[]>
addEventListener(eventName, handler)Subscribe to an event; call .remove() on the returned subscription to unsubscribeEventSubscription
removeAllListeners(eventName)Remove all listeners for an eventvoid

Events

Subscribe with addEventListener('onSpeechResults', handler) etc. Event names are plain strings.

EventPlatformsPayload
onSpeechStartiOS, Android— (recognition started)
onSpeechBeginAndroid— (user began speaking)
onSpeechEndiOS, Android{ message?: string }
onSpeechErroriOS, Android{ code: number, message: string } — code is platform-specific (negative values on iOS, SpeechRecognizer.ERROR_* values on Android)
onSpeechResultsiOS, Android{ value: string, results?: { transcriptions: Array<{ text: string, confidence: number }> } } — results is Android-only
onSpeechPartialResultsiOS, Android{ value: string, results?: { transcriptions: Array<{ text: string }> } } — results is Android-only
onSpeechVolumeChangedAndroid{ value: number } (RMS loudness in dB)
onSpeechAudioBufferAndroid{ buffer: string } (Base64-encoded raw audio)
onSpeechEventAndroid{ eventType: number, ... } (service-specific events)

Troubleshooting

The native module "VoiceToText" is not available The native side was not registered. Make sure you ran pod install on iOS and rebuilt the app after installing the package. The module is not available in Expo Go — use a development build. Since v0.4.0 this surfaces as a descriptive error at call time instead of crashing the app on import.

Speech recognition does not work on the iOS Simulator Simulator support for SFSpeechRecognizer varies across Xcode/simulator versions. Always verify speech features on a physical device.

Android build fails with Cannot add extension with name 'kotlin' Caused by Android Gradle Plugin 9 registering built-in Kotlin support. Fixed in v0.4.1 — upgrade to @ascendtis/[email protected] or later.

Android runtime errors (should be used only from the application main thread, Cannot convert argument / ArrayList) Both are fixed in the 0.3.x releases. Upgrade if you are on an older version.

Contributing

Issues and pull requests are welcome! See the issue tracker.

corepack yarn            # install dependencies (Yarn 3 via corepack)
corepack yarn prepare    # build lib/ with react-native-builder-bob
corepack yarn test       # run the Jest test suite
corepack yarn typecheck  # TypeScript
corepack yarn lint       # ESLint
corepack yarn example    # run the example app (react-native run-android / run-ios)

If Yarn reports the lockfile is out of date, run corepack yarn install once first.

License

MIT

Credits

This package is a continued fork of react-native-voice-to-text by Abhaya Kumar Sahoo (Android-only, no longer maintained). Thanks for the great starting point.

Scaffolded with create-react-native-library.

版本与发布记录

@ascendtis/react-native-voice-to-text 共发布 6 个版本,首次发布于 2025/05/29;过去 12 个月发布了 3 个版本。

版本发布日期距今
0.4.2latest2026/10/061 个月前
0.4.12026/10/061 个月前
0.4.02026/10/061 个月前
0.3.22025/05/301.4 年前
0.3.12025/05/291.4 年前
0.3.02025/05/291.4 年前

关于 @ascendtis/react-native-voice-to-text 的常见问题

@ascendtis/react-native-voice-to-text 是什么?

This can use to convert voice to text real time in device

如何安装 @ascendtis/react-native-voice-to-text?

在项目目录运行 npm install @ascendtis/react-native-voice-to-text,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(*)、react-native(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ascendtis/react-native-voice-to-text 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ascendtis/react-native-voice-to-text 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ascendtis/react-native-voice-to-text 可以商用吗?

@ascendtis/react-native-voice-to-text 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ascendtis/react-native-voice-to-text 的下载量有多少?

根据 npm 官方下载统计,@ascendtis/react-native-voice-to-text 在 2026/10/07 – 2026/11/05 这 30 天内被下载 3,281 次,日均约 109 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ascendtis/react-native-voice-to-text 还在维护吗?

最新版本 0.4.2 发布于 2026/10/06(1 个月前);至今共发布 6 个版本,其中过去 12 个月发布了 3 个;首次发布于 2025/05/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。