If you want to integrate it into your program, just need to integrate RecognizerIntent as shown below:
private static final int RECOGNITION_REQUEST_CODE = 1234;//This number is only used for recognition tag. Any number is fine. Intent it = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); it.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); //it.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); it.putExtra(RecognizerIntent.EXTRA_PROMPT, "Pleas speak..."); this.startActivityForResult(it, RECOGNITION_REQUEST_CODE);The example shown above is to call RecognizerIntent directly.
It will show up the Android defined UI.
The Speech Recognition activity will deal with the recording of voice and start to recognize it automatically.
We don't need to do much effort.
The parameter for EXTRA_LANGUAGE_MODEL can be RecognizerIntent.LANGUAGE_MODEL_FREE_FORM or RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH,
What's the difference?
LANGUAGE_MODEL_FREE_FORM: In general, we will use this parameter for most cases.
LANGUAGE_MODEL_WEB_SEARCH: It is optimized for web search and is suitable for short sentence.
However, it seems that it does not make big difference according to my test result.
Then, how can we get the recognition result?
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); String firstMatched; if(requestCode == RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK){ //The recognition result will more than one. They will be sorted so that the nearest matched result come out first. ArrayList<string> resultList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); firstMatched = (String)resultList.get(0);//We just pick up the first one } else { firstMatched = "Cannot recognize"; } result.setText(firstMatched); }In the whole process, make sure that the program can access the Internet.
If you want to do the recognition by off-line, you can down the off-line speech recognition data in advance.
Download the specified language data you want in Settings and then you can proceed speech recognition without Internet available.
However, it seems that the accuracy is poor for off-line mode.
沒有留言:
張貼留言