2.3.4 语音识别操作
## 2.3.4 语音识别操作
* Activity同样通过Intent调用进行语音识别,MainService在onStartCommand接收到REG_START,随即启动识别,接受到REG_STOP则终止识别。识别启动后,将会有各种识别的回调,如图所示:
![](image/screenshot_1496399692845.png)
* 最终目的是在onRecognizeResult(String)中获得识别的结果,然后即可进入ChatRobot的语义处理流程,如果识别出错,将回调onRecognizeError(int)方法,各种回调均会有对应的事件传送到Activity,本sdk采用了EventBus(一款针对Android优化的发布/订阅事件总线)传送事件,Activity端只需注册到默认的EventBus:
~~~
EventBus.getDefault().register(this);
~~~
* 然后即可接收相对应的事件,代码如下所示:
~~~
public void onEventMainThread(RecordUpdateEvent e){
switch(e.getState()){
case RecordUpdateEvent.RECORD_IDLE:
//voiceButton.setRecordIdleState();
voiceButton.setText("点击说话");
break;
case RecordUpdateEvent.RECORDING:
//voiceButton.setRecordStartState();
voiceButton.setText("录音中...");
break;
case RecordUpdateEvent.RECOGNIZING:
// voiceButton.setRecognizeCompletedState();
voiceButton.setText("识别中...");
break;
case RecordUpdateEvent.RECORD_IDLE_AFTER_RECOGNIZED:
voiceButton.setText("思考中...");
break;
default:break;
}
}
public void onEventMainThread(RecognizedEvent e){
if(TextUtils.isEmpty(e.getText()))return;
history.add(new Msg(e.getText(), Msg.INPUT_TYPE));
adater.notifyDataSetChanged();
recyclerView.scrollToPosition(history.size() - 1);
}
~~~