在评估了几个第三方语音识别选项后,谷歌语音识别是迄今为止最准确的。使用 Google 语音识别时有两种基本方法。最简单的方法是启动一个 Intent 并相应地处理结果:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE );
然后在您的 onActivityResults() 中,您将处理服务返回的匹配项:
/**
* Handle the results from the recognition activity.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Toast.makeText(this, "voice recog result: " + resultCode, Toast.LENGTH_LONG).show();
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
// Fill the list view with the strings the recognizer thought it could have heard
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
// handleResults
if (matches != null) {
handleResults(matches);
}
}
}
第二种方法涉及更多,但可以更好地处理识别服务运行时可能发生的错误情况。使用这种方法,您将创建自己的识别侦听器和回调方法。例如:
开始聆听:
mSpeechRecognizer.startListening(mRecognizerIntent);
其中 mRecognizerIntent:
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getBaseContext());
mSpeechRecognizer.setRecognitionListener(mRecognitionListener);
mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mRecognizerIntent.putExtra("calling_package", "com.you.package");
然后,创建您的侦听器:
private RecognitionListener mRecognitionListener = new RecognitionListener() {
public void onBufferReceived(byte[] buffer) {
// TODO Auto-generated method stub
//Log.d(TAG, "onBufferReceived");
}
public void onError(int error) {
// TODO Auto-generated method stub
// here is where you handle the error...
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
Log.d(TAG, "onEvent");
}
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
Log.d(TAG, "onPartialResults");
}
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
Log.d(TAG, "onReadyForSpeech");
}
public void onResults(Bundle results) {
Log.d(TAG, ">>> onResults");
//Toast.makeText(getBaseContext(), "got voice results!", Toast.LENGTH_SHORT);
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
handleResults(matches);
}
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
//Log.d(TAG, "onRmsChanged");
}
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
Log.d(TAG, "onBeginningOfSpeech");
}
public void onEndOfSpeech() {
// TODO Auto-generated method stub
Log.d(TAG, "onEndOfSpeech");
}
};
你可以添加你的 handleResults() 来做任何你想做的事情。