## 2.3.4 語音識別操作
* Activity同樣通過Intent調用進行語音識別,MainService在onStartCommand接收到REG_START,隨即啟動識別,接受到REG_STOP則終止識別。識別啟動后,將會有各種識別的回調,如圖所示:

* 最終目的是在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);
}
~~~