注:在线语法识别服务已下线,新用户无法使用。

//1.创建SpeechRecognizer对象
SpeechRecognizer mAsr = SpeechRecognizer.createRecognizer( );
// ABNF语法示例,可以说”北京到上海”
String mCloudGrammar = "#ABNF 1.0 UTF-8;
    languagezh-CN; 
    mode voice;
    root $main;
    $main = $place1 到$place2 ;
    $place1 = 北京 | 武汉 | 南京 | 天津 | 天京 | 东京;
    $place2 = 上海 | 合肥; ";

//2.构建语法文件
mAsr.setParameter(SpeechConstant.TEXT_ENCODING, "utf-8");
ret = mAsr.buildGrammar("abnf", mCloudGrammar ,    grammarListener);
if (ret != ErrorCode.SUCCESS){
    DebugLog.Log("语法构建失败,错误码:" + ret);
}else{
    DebugLog.Log("语法构建成功");
}
//3.开始识别,设置引擎类型为云端
mAsr.setParameter(SpeechConstant.ENGINE_TYPE, "cloud");
//设置grammarId
mAsr.setParameter(SpeechConstant.CLOUD_GRAMMAR, grammarId);
mAsr.startListening(mRecognizerListener);
if (ret != ErrorCode.SUCCESS) {
    DebugLog.Log("识别失败,错误码: " + ret);
}

//构建语法监听器
private GrammarListener grammarListener = new GrammarListener() {
    @Override
    public void onBuildFinish(String grammarId, SpeechError error) {
        if(error == null && !TextUtils.isEmpty(grammarId) ){
            //构建语法成功,请保存grammarId用于识别
        }else{
            DebugLog.Log("语法构建失败,错误码:" + error.getErrorCode());
        }        
    }
};
Copyright © iflytek.com 2018 all right reserved,powered by Gitbook该文件修订时间: 2018-07-13 09:08:20

results matching ""

    No results matching ""