美文网首页Ionic Framework
百度语音识别cordova插件

百度语音识别cordova插件

作者: hhjjj1010 | 来源:发表于2018-02-01 17:57 被阅读827次

前言

  1. 这是一个百度语音识别的cordova插件。为什么使用百度语音识别,因为是免费的,识别的准确度也还挺不错的。
  2. 这个插件只包含语音识别功能,不包含其他的比如唤醒、长语音功能。
  3. 百度语音开发文档 http://ai.baidu.com/docs#/ASR-API/top

支持平台

  1. Android
  2. iOS

安装

  1. 在线npm安装
    cordova plugin add cordova-plugin-bdasr --variable APIKEY=[your apikey] --variable SECRETKEY=[your secretkey] --variable APPID=[your appid]

  2. 在线url安装
    cordova plugin add https://github.com/hhjjj1010/cordova-plugin-bdasr.git --variable APIKEY=[your apikey] --variable SECRETKEY=[your secretkey] --variable APPID=[your appid]

  3. 本地安装
    cordova plugin add /your localpath --variable APIKEY=[your apikey] --variable SECRETKEY=[your secretkey] --variable APPID=[your appid]

API使用

 // 开启语音识别
 cordova.plugins.bdasr.startSpeechRecognize();

// 语音识别事件监听
 cordova.plugins.bdasr.addEventListener(function (res) {
    // res参数都带有一个type
    if (!res) {
      return;
    }

    switch (res.type) {
      case "asrReady": {
        // 识别工作开始,开始采集及处理数据
        $scope.$apply(function () {
          // TODO
        });
        break;
      }

      case "asrBegin": {
        // 检测到用户开始说话
        $scope.$apply(function () {
          // TODO
        });
        break;
      }

      case "asrEnd": {
        // 本地声音采集结束,等待识别结果返回并结束录音
        $scope.$apply(function () {
          // TODO
        });
        break;
      }

      case "asrText": {
        // 语音识别结果
        $scope.$apply(function () {
          var message = angular.fromJson(res.message);
          var results = message["results_recognition"];
        });
        break;
      }

      case "asrFinish": {
        // 语音识别功能完成
        $scope.$apply(function () {
          // TODO
        });
        break;
      }

      case "asrCancel": {
        // 语音识别取消
        $scope.$apply(function () {
          // TODO
        });
        break;
      }

      default:
        break;
    }

  }, function (err) {
     alert("语音识别错误");
  });

// 主动结束语音识别
cordova.plugins.bdasr.closeSpeechRecognize();

// 主动取消语音识别
cordova.plugins.bdasr.cancelSpeechRecognize();

写在最后

因为对android开发并不是很熟悉,所以特此记录在开发插件的android端时遇到的一些问题

  1. 加载so库,对应不同的平台,需要添加不同平台的.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"/>
    
  2. PermissionHelper.requestPermission()方法封装了动态获取权限的代码
    动态获取权限的回调方法:public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {})

相关文章

网友评论

  • 哈扛不住开回家:大神你好,我是做ios开发的,在cordova 下 index.html 调用 cordova.plugins.bdasr.startSpeechRecognize(); 返回识别结果在bsasr.m页面,怎么才能在cordova中取到结果呢,还有 cordova.plugins.bdasr.addEventListener(function (res) {这句话是在哪里调用呢?
  • 搬砖的哈哈:为啥我的在安卓上面刚进入采集数据,马上又进入语音识别错误,安卓的打的是测试包,在真机上运行的,
  • a8907ce03b4d:你好。github上给你提交了个Pull requests请求,麻烦你看看呗
    a8907ce03b4d:@hhjjj1010 你好,我现在重新拉去的时候提示

    Downloading src/ios/BDSClientLib/libBaiduSpeechSDK.a (170 MB)
    Error downloading object: src/ios/BDSClientLib/libBaiduSpeechSDK.a ( Smudge error: Error downloading src/ios/BDSClientLib/libBaiduSpeech9ef6777515252f7fb6462e2692bf16b751c08a62735c500ae802d35d518a81): batse: This repository is over its data quota. Purchase more data packsre access.

    Errors logged to D:\git\bdasr\.git\lfs\logs\20180927T163734.0981792.Use `git lfs logs last` to view the log.
    error: external filter 'git-lfs filter-process' failed
    fatal: src/ios/BDSClientLib/libBaiduSpeechSDK.a: smudge filter lfs fwarning: Clone succeeded, but checkout failed.
    You can inspect what was checked out with 'git status'
    and retry the checkout with 'git checkout -f HEAD'

    因该是库文件太大超过github免费的空间了,所以文件下载失败。
    hhjjj1010:已经merge
  • a8907ce03b4d:你好,在ios编译的时候提示这个错误要怎么处理呢?
    Undefined symbols for architecture arm64:
    "_OBJC_CLASS_$_CTTelephonyNetworkInfo", referenced from:
    objc-class-ref in libBaiduSpeechSDK.a(BDVRReachability.o)
    "_CTRadioAccessTechnologyHSDPA", referenced from:
    -[BDVRReachability highestAvailableConnectivity] in libBaiduSpeechSDK.a(BDVRReachability.o)
    "_CTRadioAccessTechnologyCDMA1x", referenced from:
    -[BDVRReachability highestAvailableConnectivity] in libBaiduSpeechSDK.a(BDVRReachability.o)
    "_CTRadioAccessTechnologyCDMAEVDORev0", referenced from:
    -[BDVRReachability highestAvailableConnectivity] in libBaiduSpeechSDK.a(BDVRReachability.o)
    "_CTRadioAccessTechnologyEdge", referenced from:
    -[BDVRReachability highestAvailableConnectivity] in libBaiduSpeechSDK.a(BDVRReachability.o)
    "_CTRadioAccessTechnologyWCDMA", referenced from:
    -[BDVRReachability highestAvailableConnectivity] in libBaiduSpeechSDK.a(BDVRReachability.o)
    "_CTRadioAccessTechnologyCDMAEVDORevA", referenced from:
    -[BDVRReachability highestAvailableConnectivity] in libBaiduSpeechSDK.a(BDVRReachability.o)
    "_CTRadioAccessTechnologyHSUPA", referenced from:
    -[BDVRReachability highestAvailableConnectivity] in libBaiduSpeechSDK.a(BDVRReachability.o)
    "_CTRadioAccessTechnologyGPRS", referenced from:
    -[BDVRReachability highestAvailableConnectivity] in libBaiduSpeechSDK.a(BDVRReachability.o)
    "_CTRadioAccessTechnologyCDMAEVDORevB", referenced from:
    -[BDVRReachability highestAvailableConnectivity] in libBaiduSpeechSDK.a(BDVRReachability.o)
    "_CTRadioAccessTechnologyeHRPD", referenced from:
    -[BDVRReachability highestAvailableConnectivity] in libBaiduSpeechSDK.a(BDVRReachability.o)
    ld: symbol(s) not found for architecture arm64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    ** ARCHIVE FAILED **
    hhjjj1010:@珜羽 http://www.cocoachina.com/ios/20170221/18737.html
    a8907ce03b4d:@hhjjj1010 你好,我清空了xcode的缓存,重新安装了插件,真机连上,运行你这个命令还是报这个错误。代码是同事在安卓上测试通过的。
    hhjjj1010:@珜羽 ionic cordova build ios的时候把真机联上就不会了

本文标题:百度语音识别cordova插件

本文链接:https://www.haomeiwen.com/subject/nnndzxtx.html