1 Star 1 Fork 0

walkOnly/react-native-speech-iflytek

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

react-native-speech-iflytek · npm npm GitHub closed issues

react-native-speech-iflytek 是一个 React Native 下的科大讯飞语音库,可以进行语音识别与语音合成。

Support

  • React Native >= 0.47.0 from 0.2.0
  • React Native >= 0.42.0 from 0.1.2
  • Android
  • iOS from 1.0.0

Install

yarn add react-native-speech-iflytek
react-native link

安装、链接后还须进行下面两步(以 Example 工程为例):

  1. 讯飞开放平台 下载组合服务 SDK (选择语音听写在线语音合成),分别下载 Android 与 iOS 平台 SDK。

  2. 替换 SDK 文件:

    1. 使用下载 Android SDK 的 Android_voice_xxxx_xxxxxxxx/libs 文件夹替换 Example/node_modules/react-native-speech-iflytek/android/libs 文件夹;
    2. 使用下载 iOS SDK 的 iOS_voice_xxxx_xxxxxxxx/libs 文件夹替换 Example/node_modules/react-native-speech-iflytek/ios/libs 文件夹。
  3. iOS 平台还需手动添加部分依赖库:

    1. 在 XCode 中打开 Example/ios/YourProject.xcodeproj
    2. 将讯飞框架文件 Example/node_modules/react-native-speech-iflytek/ios/libs/iflyMSC.framework 拖入 Project navigator 的 Frameworks 下,注意选择 Copy items if needed
    3. 添加讯飞依赖的系统库(见:科大讯飞MSC开发指南-iOS-集成流程):
      • CoreLocation.framework
      • CoreTelephony.framework
      • AVFoundation.framework
      • AddressBook.framework
      • Contacts.framework
      • AudioToolbox.framework
      • SystemConfiguration.framework
      • QuartzCore.framework
      • UIKit.framework
      • Foundation.framework
      • CoreGraphics.framework

4.android平台权限

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.RECORD_AUDIO" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <!--读取网络信息状态 -->
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <!--获取当前wifi状态 -->
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
 <!--允许程序改变网络连接状态 -->
 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
 <!--读取手机信息权限 -->
 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
 <uses-permission android:name="android.permission.WRITE_SETTINGS" />

Usage

(详见 Example)引入包:

import { Recognizer, Synthesizer, SpeechConstant } from "react-native-speech-iflytek";

语音识别:

Recognizer.init("57c7c5b0");
this.recognizerEventEmitter = new NativeEventEmitter(Recognizer);
this.recognizerEventEmitter.addListener('onRecognizerResult', this.onRecognizerResult);
Recognizer.start();

处理识别结果:

onRecognizerResult(e) {
    if (!e.isLast) {
        return;
    }
    this.setState({ text: e.result });
}

API

Recognizer

Methods

  • Recognizer.init(String AppId)
    初始化语音识别
  • Recognizer.start()
    开始语音识别
  • Recognizer.cancel()
    取消语音识别
  • Recognizer.isListening()
    检测当前是否正在语音识别。返回 Promise,结果为 bool 类型,表示当前是否正在语音识别
  • Recognizer.stop()
    如果正在语音识别,则结束语音识别
  • Recognizer.setParameter(String parameter, String value)
    语音识别设置,详见讯飞语音文档
  • Recognizer.getParameter(String param)
    获取语音识别设置,详见讯飞语音文档。返回 Promise,结果为 String 类型,表示语音识别设置值

Events

  • onRecognizerResult(JSON result)
    语音识别结果,在语音识别时会不断触发该事件,resultJSON 类型,其值:

    • text:当次识别结果
    • result:当前识别结果,最常使用
    • isLast:是否是最后一次识别,调用 Recognizer.stop() 后,isLast 值为 true,否则一直为 false
    • duration:当前识别时间长度
  • onRecognizerVolumeChanged(JSON result)
    语音识别的音量大小,当识别的语音改变音量时会触发该事件,resultJSON 类型,其值:

    • volume: 当前音量大小
  • onRecognizerError(JSON error)
    语音识别出现错误,错误信息与讯飞文档保持一致,其值:

    • errorCode: 获取错误码,关于错误码请见官方文档 MSC错误码
    • errorType: (仅 iOS)获取错误码类型
    • errorDesc: (仅 iOS)获取错误描述
    • errorDescription: (仅 Android)获取错误描述,不包含错误码的描述信息
    • plainDescription: (仅 Android)获取错误描述,包含错误码的描述信息

Synthesizer

Methods

  • Synthesizer.init(String AppId)
    初始化语音合成
  • Synthesizer.start(String content)
    开始语音合成
  • Synthesizer.stop()
    如果正在语音合成,则结束语音合成
  • Synthesizer.isSpeaking()
    检测当前是否正在语音合成。返回 Promise,结果为 bool 类型,表示当前是否正在语音合成
  • Synthesizer.pause()
    如果正在语音合成,则暂停语音合成(对应 resume)
  • Synthesizer.resume()
    如果正在语音合成,则开始语音合成(对应 pause)
  • Synthesizer.setParameter(String parameter, String value)
    语音合成设置,详见讯飞语音文档
  • Synthesizer.getParameter(String param)
    获取语音合成设置,详见讯飞语音文档。返回 Promise,结果为 String 类型,表示语音合成设置值

Events

  • onSynthesizerBufferCompletedEvent()
    语音合成缓冲完成时触发该事件
  • onSynthesizerSpeakCompletedEvent()
    语音合成播放完成时触发该事件

SpeechConstant

本模块包含讯飞接口的所有常量,如设置发言人、发言速度等,详见讯飞文档,使用示例:

Synthesizer.setParameter(SpeechConstant.VOICE_NAME, "xiaoyu");

Maybe

FAQ

  1. iOS 提示 iflytek.framework not found ?  (见 issue 15
  • 解决方法一(建议):Xcode 没有找到讯飞库,在项目的 Building Settings -> Search Paths -> Framework Search Paths 中手动添加 iflytek.framework 的目录 $(SRCROOT)/../node_modules/react-native-speech-iflytek/ios/libs
  • 解决方法二:将 iflytek.framework 拖入时注意选择 Copy items if needed。如忘记选择,最好的方式是在工程中删除讯飞框架,将讯飞框架移至其他文件夹,并重新拖入工程,选择 Copy items if needed。(Xcode 的缓存会记住上次选择,若第一次没有Copy items if needed,在同一位置下重新拖入不会再次出现选择框。)
  1. react-native [command] 命令失效?
    同时使用 yarn 和 npm 时可能会出现这个问题,建议再敲一次 yarn 命令解决依赖。

Contribute

期待提出有关建议,欢迎做出贡献,感谢 star。
Github: https://github.com/zphhhhh/react-native-speech-iflytek

MIT License Copyright (c) 2017 zphhhhh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

React Native 下的科大讯飞语音库,可以进行语音识别与语音合成 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Objective-C
1
https://gitee.com/walkOnly/react-native-speech-iflytek.git
git@gitee.com:walkOnly/react-native-speech-iflytek.git
walkOnly
react-native-speech-iflytek
react-native-speech-iflytek
master

搜索帮助