1:申请相关appid,见官网,添加语音听写服务,下载sdk 2:下载cordova插件,见https://github.com/Edc-zhang/cordova-plugin-IFlyspeech 3:将sdk的libs文件夹,替换cordova插件的src/android/libs 将cordova插件的
1:申请相关appid,见官网,添加语音听写服务,下载sdk
2:下载cordova插件,见https://github.com/Edc-zhang/cordova-plugin-IFlyspeech
3:将sdk的libs文件夹,替换cordova插件的src/android/libs
将cordova插件的plugin.xml、src\android\res\values\string.xml中的appid相应替换
4:cordova6 可以直接安装 ionic cordova plugin add 路径
cordova7需要添加plugman。安装命令npm install -g plugman,然后 plugman createpackagejson 插件全路径,然后就生成package.json了
5:新建ionic项目
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <plugin id="cordova-plugin-xunfeiListenSpeaking" version="0.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android"> <name>cordova-plugin-xunfeiListenSpeaking</name> <platform name = "android"> <js-module name="xunfeiListenSpeaking" src="www/cordova-plugin-xunfeiListenSpeaking.js"> <clobbers target="xunfeiListenSpeaking" /> </js-module> <config-file target="res/xml/config.xml" parent="/*"> <feature name="XunfeiListenSpeaking" > <param name="android-package" value="com.thomas.xunfeilistenspeaking.XunfeiListenSpeaking"/> </feature> </config-file> <config-file target="AndroidManifest.xml" parent="/manifest"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <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.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- 移动统计分析 --> <meta-data android:name="IFLYTEK_APPKEY" android:value="5c0f2378" /> <meta-data android:name="IFLYTEK_CHANNEL" android:value="Android_Demo" /> </config-file> <config-file target="AndroidManifest.xml" parent="/manifest/application"> <activity android:name="com.thomas.xunfeilistenspeaking.XunfeiDialogActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar"> </activity> </config-file> <!--assets--> <source-file src="src/android/assets/iattest.wav" target-dir="app/src/main/assets"/> <source-file src="src/android/assets/call.bnf" target-dir="app/src/main/assets"/> <source-file src="src/android/assets/grammar_sample.abnf" target-dir="app/src/main/assets"/> <source-file src="src/android/assets/keys" target-dir="app/src/main/assets"/> <source-file src="src/android/assets/userwords" target-dir="assets"/> <source-file src="src/android/assets/iflytek/voice_bg.9.png" target-dir="app/src/main/assets/iflytek"/> <source-file src="src/android/assets/iflytek/voice_empty.png" target-dir="app/src/main/assets/iflytek"/> <source-file src="src/android/assets/iflytek/voice_full.png" target-dir="app/src/main/assets/iflytek"/> <source-file src="src/android/assets/iflytek/waiting.png" target-dir="app/src/main/assets/iflytek"/> <source-file src="src/android/assets/iflytek/warning.png" target-dir="app/src/main/assets/iflytek"/> <source-file src="src/android/assets/iflytek/recognize.xml" target-dir="app/src/main/assets/iflytek"/> <!-- <source-file src="src/android/assets/iattest.wav" target-dir="assets"/> <source-file src="src/android/assets/call.bnf" target-dir="assets"/> <source-file src="src/android/assets/grammar_sample.abnf" target-dir="assets"/> <source-file src="src/android/assets/keys" target-dir="assets"/> <source-file src="src/android/assets/userwords" target-dir="assets"/> <source-file src="src/android/assets/iflytek/voice_bg.9.png" target-dir="assets/iflytek"/> <source-file src="src/android/assets/iflytek/voice_empty.png" target-dir="assets/iflytek"/> <source-file src="src/android/assets/iflytek/voice_full.png" target-dir="assets/iflytek"/> <source-file src="src/android/assets/iflytek/waiting.png" target-dir="assets/iflytek"/> <source-file src="src/android/assets/iflytek/warning.png" target-dir="assets/iflytek"/> <source-file src="src/android/assets/iflytek/recognize.xml" target-dir="assets/iflytek"/> --> <!--lib--> <!-- 移植到老项目 --> <!-- <lib-file src="src/android/libs/Msc.jar" target-dir="libs"/> <lib-file src="src/android/libs/Sunflower.jar" target-dir="libs"/> <source-file src="src/android/libs/armeabi/libmsc.so" target-dir="app/libs/armeabi"/> <source-file src="src/android/libs/arm64-v8a/libmsc.so" target-dir="app/libs/arm64-v8a"/> <source-file src="src/android/libs/armeabi-v7a/libmsc.so" target-dir="app/libs/armeabi-v7a"/> <source-file src="src/android/libs/mips/libmsc.so" target-dir="app/libs/mips"/> <source-file src="src/android/libs/mips64/libmsc.so" target-dir="app/libs/mips64"/> <source-file src="src/android/libs/x86/libmsc.so" target-dir="app/libs/x86"/> <source-file src="src/android/libs/x86_64/libmsc.so" target-dir="app/libs/x86_64"/> --> <!-- 新建项目 --> <lib-file src="src/android/libs/Msc.jar" target-dir="libs"/> <lib-file src="src/android/libs/Sunflower.jar" target-dir="libs"/> <source-file src="src/android/libs/armeabi/libmsc.so" target-dir="app/src/main/jniLibs/armeabi"/> <source-file src="src/android/libs/arm64-v8a/libmsc.so" target-dir="app/src/main/jniLibs/arm64-v8a"/> <source-file src="src/android/libs/armeabi-v7a/libmsc.so" target-dir="app/src/main/jniLibs/armeabi-v7a"/> <!--layout--> <source-file src="src/android/res/layout/activity_xunfei_dialog.xml" target-dir="res/layout"/> <!--xml--> <source-file src="src/android/res/xml/iat_setting.xml" target-dir="res/xml"/> <source-file src="src/android/res/xml/ise_settings.xml" target-dir="res/xml"/> <source-file src="src/android/res/xml/tts_setting.xml" target-dir="res/xml"/> <source-file src="src/android/res/xml/understand_setting.xml" target-dir="res/xml"/> <!--src--> <source-file src="src/android/src/com/thomas/xunfeilistenspeaking/XunfeiListenSpeaking.java" target-dir="src/com/thomas/xunfeilistenspeaking"/> <source-file src="src/android/src/com/thomas/xunfeilistenspeaking/IatSettings.java" target-dir="src/com/thomas/xunfeilistenspeaking"/> <source-file src="src/android/src/com/thomas/xunfeilistenspeaking/JsonParser.java" target-dir="src/com/thomas/xunfeilistenspeaking"/> <source-file src="src/android/src/com/thomas/xunfeilistenspeaking/SettingTextWatcher.java" target-dir="src/com/thomas/xunfeilistenspeaking"/> <source-file src="src/android/src/com/thomas/xunfeilistenspeaking/XunfeiDialogActivity.java" target-dir="src/com/thomas/xunfeilistenspeaking"/> <!--res--> <config-file target="res/values/strings.xml" parent="/resources"> <!--<string name="app_name">讯飞语音示例</string>--> <!-- 请替换成在语音云官网申请的appid --> <string name="app_id">5c0f2378</string> <string name="example_explain">本示例为讯飞语音Android平台开发者提供语音听写、语法识别、语义理解和语音合成等代码样例,旨在让用户能够依据该示例快速开发出基于语音接口的应用程序。</string> <string name="text_tts_source">科大讯飞作为中国最大的智能语音技术提供商,在智能语音技术领域有着长期的研究积累,并在中文语音合成、语音识别、口语评测等多项技术上拥有国际领先的成果。科大讯飞是我国唯一以语音技术为产业化方向的“国家863计划成果产业化基地”…</string> <string name="text_tts_source_en">iFLYTEK is a national key software enterprise dedicated to the research of intelligent speech and language technologies, development of software and chip products, provision of speech information services, and integration of E-government systems. The intelligent speech technology of iFLYTEK, the core technology of the company, represents the top level in the world.</string> <string name="text_isr_abnf_hint">\t上传内容为:\n\t#ABNF 1.0 gb2312;\n\tlanguage zh-CN;\n\tmode voice;\n\troot $main;\n\t$main = $place1 到$place2 ;\n\t$place1 = 北京 | 武汉 | 南京 | 天津 | 东京;\n\t$place2 = 上海 | 合肥;</string> <string name="text_understand_hint">\t您可以说:\n\t今天的天气怎么样?\n\t北京到上海的火车?\n\t有什么好吃的?\n\t上海外滩有哪些酒店?\n\n\t更多语义请登录:\n\thttp://osp.voicecloud.cn/ \n\t配置您的专属语义吧!</string> <!-- 听写 --> <string name="text_begin">请开始说话…</string> <string name="text_begin_recognizer">开始音频流识别</string> <string name="text_upload_contacts">上传联系人</string> <string name="text_upload_userwords">上传用户词表</string> <string name="text_upload_success">上传成功</string> <string name="text_userword_empty">词表下载失败或内容为空</string> <string name="text_download_success">下载成功</string> <string name="pref_key_iat_show">iat_show</string> <string name="pref_title_iat_show">显示听写界面</string> <string name="pref_title_iat_dwa">结果动态修正</string> <string name="xunfei_cancel_listen">取消语音</string> <!-- 合成 --> <string-array name="engine_entries"> <item>本地合成</item> <item>在线合成</item> </string-array> <string-array name="engine_values"> <item>local</item> <item>cloud</item> </string-array> <string-array name="voicer_cloud_entries"> <item>小燕—女青、中英、普通话</item> <item>小宇—男青、中英、普通话</item> <item>凯瑟琳—女青、英</item> <item>亨利—男青、英</item> <item>玛丽—女青、英</item> <item>小研—女青、中英、普通话</item> <item>小琪—女青、中英、普通话</item> <item>小峰—男青、中英、普通话</item> <item>小梅—女青、中英、粤语</item> <item>小莉—女青、中英、台湾普通话</item> <item>小蓉—女青、中、四川话</item> <item>小芸—女青、中、东北话</item> <item>小坤—男青、中、河南话</item> <item>小强—男青、中、湖南话</item> <item>小莹—女青、中、陕西话</item> <item>小新—男童、中、普通话</item> <item>楠楠—女童、中、普通话</item> <item>老孙—男老、中、普通话</item> </string-array> <string-array name="voicer_cloud_values"> <item>xiaoyan</item> <item>xiaoyu</item> <item>catherine</item> <item>henry</item> <item>vimary</item> <item>vixy</item> <item>xiaoqi</item> <item>vixf</item> <item>xiaomei</item> <item>xiaolin</item> <item>xiaorong</item> <item>xiaoqian</item> <item>xiaokun</item> <item>xiaoqiang</item> <item>vixying</item> <item>xiaoxin</item> <item>nannan</item> <item>vils</item> </string-array> <string-array name="emot_entries"> <item>正常</item> <item>高兴</item> <item>悲伤</item> <item>生气</item> </string-array> <string-array name="emot_values"> <item>neutral</item> <item>happy</item> <item>sad</item> <item>angry</item> </string-array> <string-array name="stream_entries"> <item>通话</item> <item>系统</item> <item>铃声</item> <item>音乐</item> <item>闹铃</item> <item>通知</item> </string-array> <string-array name="stream_values"> <item>0</item> <item>1</item> <item>2</item> <item>3</item> <item>4</item> <item>5</item> </string-array> <string formatted="false" name="tts_toast_format">缓冲进度为%d%%,播放进度为%d%%</string> <!-- 语言 --> <string-array name="language_entries"> <item>普通话</item> <item>粤语</item> <item>河南话</item> <item>英语</item> </string-array> <string-array name="language_values"> <item>mandarin</item> <item>cantonese</item> <item>henanese</item> <item>en_us</item> </string-array> <!-- 语音评测 --> <string name="text_en_word">"[word]\napple\nbanana\norange"</string> <string name="text_en_sentence">"The quick brown fox jumps over the lazy dog."</string> <string name="text_cn_syllable">"知,痴,是"</string> <string name="text_cn_word">"磁铁,率领,脆弱,动手,古筝"</string> <string name="text_cn_sentence">"一座座雪峰插入云霄,峰顶银光闪闪,大大小小的湖泊,像颗颗宝石镶嵌在彩带般的沟谷中。"</string> <string-array name="ise_language_entries"> <item>英语</item> <item>汉语</item> </string-array> <string-array name="ise_language_values"> <item>en_us</item> <item>zh_cn</item> </string-array> <string-array name="category_entries"> <item>单字</item> <item>词语</item> <item>句子</item> </string-array> <string-array name="category_values"> <item>read_syllable</item> <item>read_word</item> <item>read_sentence</item> </string-array> <string-array name="result_level_entries"> <item>plain</item> <item>complete</item> </string-array> <!-- 标点符号 --> <string-array name="punc_entries"> <item>有标点</item> <item>无标点</item> </string-array> <string-array name="punc_values"> <item>1</item> <item>0</item> </string-array> <string-array name="dwa_entries"> <item>开启</item> <item>关闭</item> </string-array> </config-file> </platform> <platform name="ios"> <js-module name="speech" src="www/Speech.js"> <!-- <clobbers target="navigator.speech" />--> <clobbers target="xunfeiListenSpeaking" /> </js-module> <config-file parent="/*" target="config.xml"> <feature name="Speech"> <param name="ios-package" value="CDVSpeech" /> </feature> </config-file> <config-file target="*-Info.plist" parent="NSMicrophoneUsageDescription"> <string></string> </config-file> <header-file src="src/ios/CDVSpeech.h" /> <source-file src="src/ios/CDVSpeech.m" /> <framework src="AVFoundation.framework" /> <framework src="AddressBook.framework" /> <framework src="AudioToolbox.framework" /> <framework src="SystemConfiguration.framework" /> <framework src="QuartzCore.framework" /> <framework src="libz.tbd" /> <framework src="libc++.tbd" /> <framework src="UIKit.framework" /> <framework src="CoreGraphics.framework" /> <framework src="Foundation.framework" /> <framework src="CoreTelephony.framework" /> <framework src="CoreLocation.framework" /> <framework src="Contacts.framework" /> <framework src="src/ios/iflyMSC.framework" custom="true" /> </platform> </plugin>
6:用法、home.ts
1 import { Component, ChangeDetectorRef } from ‘@angular/core‘; 2 import { NavController,ToastController } from ‘ionic-angular‘; 3 declare let xunfeiListenSpeaking: any; 4 5 @Component({ 6 selector: ‘page-home‘, 7 templateUrl: ‘home.html‘ 8 }) 9 export class HomePage { 10 public listenText: string = ‘‘; 11 12 13 constructor(public navCtrl: NavController, public toastCtrl: ToastController, public cd: ChangeDetectorRef) { 14 console.log(xunfeiListenSpeaking); 15 16 } 17 18 single(){ 19 let that = this; 20 xunfeiListenSpeaking.startListen(data => { 21 if (data) { 22 //data即是返回的文字 23 that.listenText = data; 24 console.log(data); 25 } else { 26 console.log(data); 27 console.log(‘没有值‘); 28 } 29 setTimeout( 30 function() { 31 //事件处理 32 that.listenText = data; 33 console.log(‘结束说话‘); 34 xunfeiListenSpeaking.stopListen(); 35 //that.refresh(); 36 console.log(that.listenText); 37 }, 5000) 38 39 }, err => alert(err), false, true); 40 41 console.log(that.listenText); 42 43 } 44 45 testVoice1() { 46 let that = this; 47 console.log(‘开始说话...‘); 48 const toast = this.toastCtrl.create({ 49 message: ‘请开始说话‘, 50 duration: 3000 51 }); 52 toast.present(); 53 xunfeiListenSpeaking.startListen(data=>{ 54 if (data) { 55 //data即是返回的文字 56 that.listenText = data; 57 console.log(data); 58 } else { 59 console.log(data); 60 console.log(‘没有值‘); 61 } 62 }, err=>alert(err), false, true); 63 console.log(that.listenText); 64 } 65 66 stopListen() { 67 setTimeout( 68 function() { 69 //事件处理 70 console.log(‘结束说话‘); 71 xunfeiListenSpeaking.stopListen(); 72 },5000) 73 // alert(this.listenText); 74 } 75 output(){ 76 alert(this.listenText); 77 } 78 79 testVoice2() { 80 // let msg = ‘你好‘; 81 xunfeiListenSpeaking.startSpeak(data => alert(data), err => alert(err), this.listenText); 82 } 83 84 }
home.html
1 <ion-header> 2 <ion-navbar> 3 <ion-title>Home</ion-title> 4 </ion-navbar> 5 </ion-header> 6 <ion-content padding> 7 <h2>Welcome to Ionic!</h2> 8 <button ion-button (touchstart)="testVoice1()" (touchend)="stopListen()">按住说话</button> 9 <input type="text" [(ngModel)]="listenText" /> 10 <button ion-button (click)="testVoice2()">语音读出</button> 11 <button ion-button (click)="output()">输出文字</button> 12 <button ion-button (click)="single()">single</button> 13 </ion-content>
7:效果: