反应原声
语音对文本库 原生反应 与 新建建筑(Fabric/TurboModules) 财务报告和审定财务报表 无桥模式 支助.
说明: 这个图书馆是一个叉子 @ react-native-voice/voice (英语) 包含针对 React Industrial 0.76+ 和 New Architecture 的自定义修正. 在 GitHub 上查看来源
有什么不同吗?
此分叉包括了与原始库相比的下列改进:
- ✅ 新架构支持 - 与Fabric和TurboModules合作
- ✅ 无桥模式 - 全力支持React Industrial无桥模式
- ✅ 反应原生 0.76+ - 测试并使用最新的RN版本
- ✅ 固定安卓事件 - 事件在新架构中适当到达 JavaScript
- ✅ 固定 iOS 涡轮模块注册 - iOS的适当倒置处理
- ✅ 改进了地方处理 - 更好地支持印巴地区语言
- ✅ 清除类型脚本类型 - 更新类型定义
安装
yarn add @dev-amirzubair/react-native-voice
# or
npm install @dev-amirzubair/react-native-voice --save
iOS 设置
cd ios && pod install
Android 设置
不需要额外的设置 - 自动链接处理所有事务 .
使用量
import Voice from '@dev-amirzubair/react-native-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);
// Start listening
await Voice.start('en-US');
// Stop listening
await Voice.stop();
// Clean up
await Voice.destroy();
完整示例
import React, { useEffect, useState, useCallback } from 'react';
import { View, Text, Button } from 'react-native';
import Voice from '@dev-amirzubair/react-native-voice';
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 (
<View>
<Text>{isListening ? '🎤 Listening...' : 'Press Start'}</Text>
<Text>{results.join(' ')}</Text>
<Button title="Start" onPress={startListening} />
<Button title="Stop" onPress={stopListening} />
</View>
);
}
API 密码
| 方法 | 说明 | 平台 |
|---|---|---|
Voice.isAvailable() | 检查是否有语音识别功能 | Android, iOS (英语) |
Voice.start(locale) | 开始听演讲 | Android, iOS (英语) |
Voice.stop() | 别听了 | Android, iOS (英语) |
Voice.cancel() | 取消语音识别 | Android, iOS (英语) |
Voice.destroy() | 摧毁识别器实例 | Android, iOS (英语) |
Voice.removeAllListeners() | 删除全部事件听众 | Android, iOS (英语) |
Voice.isRecognizing() | 检查当前是否识别 | Android, iOS (英语) |
Voice.getSpeechRecognitionServices() | 获取可用的语音引擎 | 只有机器人 |
活动
| 活动 | 说明 | 数据 |
|---|---|---|
onSpeechStart | 开始语音识别 | { error: false } |
onSpeechEnd | 语音识别结束 | { error: false } |
onSpeechResults | 收到最后结果 | { value: ['recognized text'] } |
onSpeechPartialResults | 部分结果(活性) | { value: ['partial text'] } |
onSpeechError | 发生错误 | { error: { code, message } } |
onSpeechVolumeChanged | 音量/音量改变 | { value: number } |
onSpeechRecognized | 发言获得认可 | { isFinal: boolean } |
权限
机器人
添加为 AndroidManifest.xml数字 :
<uses-permission android:name="android.permission.RECORD_AUDIO" />
库在开始识别时自动请求权限.
iOS系统
添加为 Info.plist数字 :
<key>NSMicrophoneUsageDescription</key>
<string>This app needs microphone access for speech recognition</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>This app needs speech recognition access</string>
平台说明
机器人
- 用户停止发言后自动停止
- 要求 Google 搜索应用程序对大多数设备的语音识别
- 检查可用的服务
Voice.getSpeechRecognitionServices()
iOS系统
- 不自动停止 - 呼叫
Voice.stop()完成时 - 语音识别只在 物理装置 (不是模拟器)
- 需要 iOS 10+ 软件
博览支持
此库需要自定义本地代码, 无法与 Expo Go 一同使用 。 使用一个 发展 或弹出.
添加到您的 app.json数字 :
{
"expo": {
"plugins": ["@dev-amirzubair/react-native-voice"]
}
}
解决问题
Android: 找不到语音识别服务
安装 Google 搜索应用程序 从游戏商店.
iOS:不使用模拟器的语音识别
使用物理iOS设备——模拟器不支持语音识别.
未开火的事件
确保设置事件处理器 在此之前 电话 Voice.start()。 。 。 .
贷项
这个图书馆基于 @ react-native-voice/voice (英语) 由"回声原声"撰稿人出品. 特别感谢:
- @wenkesj (原始内容存档于2013-10-12)
- @ jamsch (英语)
- 所有原始贡献者
许可证
麻省理工学院