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

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

这可以用来在设备中将语音实时转换为文本

中文使用文档
文档目录15 节
  1. 特征
  2. 所需资源
  3. 安装
  4. 权限
  5. iOS系统
  6. 机器人
  7. 使用量
  8. 切换识别语言
  9. API 参考
  10. 方法
  11. 活动
  12. 解决问题
  13. 捐款
  14. 许可证
  15. 贷项

@ ascendtis/ react-native-voice-to-text (英语)

npm version npm downloads license

使用iOS和Android的本地语音识别能力实时将语音转换为文本.

这个包裹是未保存的 react-native-voice-to-text,在完全iOS支持下重建,新架构(TurboModule)兼容性,并持续维护.

特征

  • IOS上的原生语音识别(SFSpeechRecognizer)和Android(英语:SpeechRecognizer(中文(简体) )
  • 用户说话时实时部分结果
  • 带有信心分数的最后抄写(Android)
  • 体积水平检测(Android)
  • 多语言支持:查询、列表和切换识别语言
  • 新建筑已准备就绪( 有代码的 TurboModule)
  • 以类型定义写入脚本

所需资源

平台最低
iOS系统第15.1条
机器人API 24 (Android 7.0) (英语)
原生反应0.78+(建造和试验,对照0.78.1)

说明: 此软件包包含本地代码, 因此无法在 博览去使用一个 发展 或者一个赤裸裸的反射原生项目.

安装

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

或者说

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

该软件包在两个平台上自动链接. 在iOS上,安装CocoaPods依赖:

cd ios && pod install

然后重建您的应用程序, 以便本地模块注册 .

权限

iOS系统

将以下密钥添加到您的 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>

机器人

向您添加以下权限 AndroidManifest.xml数字 :

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

对于Android 6.0+(API 23),您还需要在运行时请求权限:

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

使用量

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

切换识别语言

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 参考

所有函数都命名为软件包的导出. 如果本地模块不可用(例如应用程序安装后没有重建),或 pod install ,每个函数都会出现描述性错误,解释如何修复设置.

方法

方法说明返回类型
startListening()开始语音识别Promise<string>
stopListening()停止语音识别Promise<string>
destroy()清理语音识别资源Promise<string>
getRecognitionLanguage()获取当前识别语言Promise<string>
setRecognitionLanguage(languageTag)设置识别语言( 如. 'en-US'(中文(简体) )Promise<boolean>
isRecognitionAvailable()检查设备是否有语音识别功能Promise<boolean>
getSupportedLanguages()获取支持的语言代码列表Promise<string[]>
addEventListener(eventName, handler)订阅事件; 调用 .remove() 返回的订阅EventSubscription
removeAllListeners(eventName)删除事件的所有听众void

活动

订阅 addEventListener('onSpeechResults', handler) 包括: 事件名称是简单的字符串 .

活动平台有效载荷
onSpeechStartiOS, Android 软件- (承认开始)
onSpeechBegin机器人- (用户开始说话)
onSpeechEndiOS, Android 软件{ message?: string }
onSpeechErroriOS, Android 软件{ code: number, message: string } —— 说 code 是针对平台的(iOS上的负值, SpeechRecognizer.ERROR_* android上数值)
onSpeechResultsiOS, Android 软件{ value: string, results?: { transcriptions: Array<{ text: string, confidence: number }> } } —— 说 results 仅为Android
onSpeechPartialResultsiOS, Android 软件{ value: string, results?: { transcriptions: Array<{ text: string }> } } —— 说 results 仅为Android
onSpeechVolumeChanged机器人{ value: number } (RMS 在dB中发声)
onSpeechAudioBuffer机器人{ buffer: string } (Base64-编码原始音频)
onSpeechEvent机器人{ eventType: number, ... } (特定服务活动)

解决问题

The native module "VoiceToText" is not available 土著方面没有登记。 记得跑 pod install 在iOS上,并在安装了软件包后重建了应用程序. 博览会Go没有这个模块——使用一个开发建筑. 自 v. 4. 0 起, 此显示为调用时的描述错误, 而不是在导入时崩溃 .

语音识别对 iOS 模拟器无效 模拟支持 SFSpeechRecognizer 不同Xcode/模拟器版本不同. 总是在物理设备上验证语音特性.

Android 构建失败 Cannot add extension with name 'kotlin' 由Android Gradle插件9 注册内置的克特林支持导致. 固定在v. 4.1——升级至 @ascendtis/[email protected] 或后会.

Android 运行时出错(should be used only from the application main thread, (中文(简体) ), Cannot convert argument 编号 : ArrayList(中文(简体) ) 两者均固定在0.3.x释放中. 如果您在旧版本上, 则升级 .

捐款

欢迎提问和拉票请求! 见 问题跟踪器。 。 。 .

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)

如果Yarn报告锁文件已过期, 请运行 corepack yarn install 一次第一个.

许可证

麻省理工学院

贷项

这个软件包是连续叉 react-native-voice-to-text 以 阿布哈亚·库马尔·萨胡 (仅Android,不再维护). 感谢伟大的起点.

脚手架与 创建- 反应- 本土- 图书馆。 。 。 .

版本与发布记录

@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 是什么?

这可以用来在设备中将语音实时转换为文本

如何安装 @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 和提交记录判断。