项目简介
本项目是基于React Native框架开发的语音识别应用,集成了讯飞语音识别SDK,支持Android和iOS平台。用户可进行语音输入并实时获取识别结果,为需要语音交互的移动应用场景提供简单易用的解决方案。
项目的主要特性和功能
- 跨平台支持:支持Android和iOS平台,实现一次开发多平台部署。
- 讯飞语音识别:集成讯飞语音识别SDK,提供高精度的语音识别服务。
- 实时语音识别:用户语音输入时,实时返回识别结果。
- 自定义配置:支持自定义语音识别的参数配置,如语言、方言、识别模式等。
- 错误处理:提供详细的错误处理机制,确保语音识别过程中的稳定性。
- 开发者调试工具:集成Flipper调试工具,方便开发者进行调试和性能优化。
安装使用步骤
1. 安装依赖
通过npm安装项目依赖:
sh
npm install
2. 配置Android环境
2.1 添加ProGuard规则
在android/app/proguard-rules.pro
文件中添加以下规则:
-keep class com.iflytek.**{*;}
-keepattributes Signature
2.2 初始化Flipper
在android/app/src/debug/java/com/example/reactnativexfspeech/ReactNativeFlipper.java
中初始化Flipper,并添加必要的插件。
3. 配置iOS环境
3.1 添加库
在Xcode中,打开项目target->build phases->Link Binary With Libraries
,添加以下库:
iflyMSC.framework
libz.tbd
AVFoundation.framework
SystemConfiguration.framework
Foundation.framework
CoreTelephony.framework
AudioToolbox.framework
UIKit.framework
CoreLocation.framework
Contacts.framework
AddressBook.framework
QuartzCore.framework
CoreGraphics.framework
libc++.tbd
3.2 设置Framework Search Paths
在Framework Search Paths
中添加:
"$(SRCROOT)/../node_modules/@zhangliang_code/react-native-xfspeech/ios/lib"
3.3 设置Bitcode
在Xcode中,关闭Bitcode设置:
Targets - Build Settings 中搜索Bitcode,设置为NO
3.4 用户隐私权限配置
在info.plist
中添加以下权限配置:
xml
<key>NSMicrophoneUsageDescription</key>
<string></string>
<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSContactsUsageDescription</key>
<string></string>
4. 运行项目
在项目根目录下运行以下命令启动应用:
sh
npm run android # 运行Android应用
npm run ios # 运行iOS应用
5. 使用语音识别功能
在应用中使用以下代码初始化和启动语音识别: ```js import React, { useEffect } from 'react'; import { StyleSheet, Text, TouchableOpacity } from 'react-native'; import { init, onBeginOfSpeech, onEndOfSpeech, onError, onResult, start, } from 'react-native-xfspeech';
export default function App() {
useEffect(() => {
init('你的appId').then(() => {
console.log('initSdk');
});
let onBeginOfSpeechListener = onBeginOfSpeech(() => {
console.log('onBeginOfSpeech');
});
let onEndOfSpeechListener = onEndOfSpeech(() => {
console.log('onEndOfSpeech');
});
let onResultListener = onResult((event) => {
console.log('onResult', event);
});
let onErrorListener = onError((event) => {
console.log('onError', event);
});
return () => {
onBeginOfSpeechListener.remove();
onEndOfSpeechListener.remove();
onResultListener.remove();
onErrorListener.remove();
};
}, []);
return (
const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, }); ```
下载地址
点击下载 【提取码: 4003】【解压密码: www.makuang.net】