前言
- 這是一個(gè)百度語(yǔ)音識(shí)別的cordova插件奕纫。為什么使用百度語(yǔ)音識(shí)別,因?yàn)槭敲赓M(fèi)的烫沙,識(shí)別的準(zhǔn)確度也還挺不錯(cuò)的匹层。
- 這個(gè)插件只包含語(yǔ)音識(shí)別功能,不包含其他的比如喚醒锌蓄、長(zhǎng)語(yǔ)音功能升筏。
- 百度語(yǔ)音開(kāi)發(fā)文檔 http://ai.baidu.com/docs#/ASR-API/top
支持平臺(tái)
- Android(bdasr_V3_20190515_c9eed5d.jar)
- iOS
安裝
在線npm安裝
本地安裝
github文件超過(guò)100M之后只能使用LFS才能上傳,但是同時(shí)也有帶寬限制瘸爽。為了避免各種限制您访,所以只能辛苦各位自行下載libBaiduSpeech.a,并放到插件的src/ios/BDSClientLib目錄中剪决。
- 第一步灵汪,將插件下載到本地
- 第二步,下載iOS SDK中缺少的libBaiduSpeech.a
- 第三步柑潦,添加libBaiduSpeech.a到src/ios/BDSClientLib
- 第四步享言,安裝插件
cordova plugin add /your localpath --variable APIKEY=your apikey --variable SECRETKEY=your secretkey --variable APPID=your appid
在線npm安裝(推薦)
在線npm安裝不受任何限制,可直接安裝妒茬。
cordova plugin add cordova-plugin-bdasr --variable APIKEY=your apikey --variable SECRETKEY=your secretkey --variable APPID=your appid
API使用
開(kāi)啟語(yǔ)音識(shí)別
startSpeechRecognize
代碼示例
cordova.plugins.bdasr.startSpeechRecognize();
關(guān)閉語(yǔ)音識(shí)別
closeSpeechRecognize
代碼示例
cordova.plugins.bdasr.closeSpeechRecognize();
取消語(yǔ)音識(shí)別
cancelSpeechRecognize
代碼示例
cordova.plugins.bdasr.cancelSpeechRecognize();
事件監(jiān)聽(tīng)
addEventListener
代碼示例
// 語(yǔ)音識(shí)別事件監(jiān)聽(tīng)
cordova.plugins.bdasr.addEventListener(function (res) {
// res參數(shù)都帶有一個(gè)type
if (!res) {
return;
}
switch (res.type) {
case "asrReady": {
// 識(shí)別工作開(kāi)始担锤,開(kāi)始采集及處理數(shù)據(jù)
$scope.$apply(function () {
// TODO
});
break;
}
case "asrBegin": {
// 檢測(cè)到用戶開(kāi)始說(shuō)話
$scope.$apply(function () {
// TODO
});
break;
}
case "asrEnd": {
// 本地聲音采集結(jié)束,等待識(shí)別結(jié)果返回并結(jié)束錄音
$scope.$apply(function () {
// TODO
});
break;
}
case "asrText": {
// 語(yǔ)音識(shí)別結(jié)果
$scope.$apply(function () {
var message = angular.fromJson(res.message);
var results = message["results_recognition"];
});
break;
}
case "asrFinish": {
// 語(yǔ)音識(shí)別功能完成
$scope.$apply(function () {
// TODO
});
break;
}
case "asrCancel": {
// 語(yǔ)音識(shí)別取消
$scope.$apply(function () {
// TODO
});
break;
}
default:
break;
}
}, function (err) {
alert("語(yǔ)音識(shí)別錯(cuò)誤");
});
寫在最后
因?yàn)閷?duì)android開(kāi)發(fā)并不是很熟悉乍钻,所以特此記錄在開(kāi)發(fā)插件的android端時(shí)遇到的一些問(wèn)題
- 加載so庫(kù)肛循,對(duì)應(yīng)不同的平臺(tái),需要添加不同平臺(tái)的.so文件
<source-file src="src/android/libs/armeabi/libBaiduSpeechSDK.so" target-dir="libs/armeabi"/>
<source-file src="src/android/libs/armeabi/libvad.dnn.so" target-dir="libs/armeabi"/>
<source-file src="src/android/libs/x86_64/libBaiduSpeechSDK.so" target-dir="libs/x86_64"/>
<source-file src="src/android/libs/x86_64/libvad.dnn.so" target-dir="libs/x86_64"/>
<source-file src="src/android/libs/x86/libBaiduSpeechSDK.so" target-dir="libs/x86"/>
<source-file src="src/android/libs/x86/libvad.dnn.so" target-dir="libs/x86"/>
<source-file src="src/android/libs/arm64-v8a/libBaiduSpeechSDK.so" target-dir="libs/arm64-v8a"/>
<source-file src="src/android/libs/arm64-v8a/libvad.dnn.so" target-dir="libs/arm64-v8a"/>
<source-file src="src/android/libs/armeabi-v7a/libBaiduSpeechSDK.so" target-dir="libs/armeabi-v7a"/>
<source-file src="src/android/libs/armeabi-v7a/libvad.dnn.so" target-dir="libs/armeabi-v7a"/>
- PermissionHelper.requestPermission()方法封裝了動(dòng)態(tài)獲取權(quán)限的代碼
動(dòng)態(tài)獲取權(quán)限的回調(diào)方法:
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {})