Dopo aver valutato diverse 3rd opzioni di riconoscimento vocale del partito, Google riconoscimento vocale è da lontano il più preciso. Esistono due approcci di base quando si utilizza il riconoscimento vocale di Google. Il metodo più semplice è quello di lanciare un Intent e gestire i risultati di conseguenza:
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);
poi nei tuoi onActivityResults(), si dovrebbe gestire le partite restituiti dal servizio:
/**
* 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);
}
}
}
Il secondo approccio è più coinvolto ma consente una migliore gestione di una condizione di errore che può verificarsi mentre il servizio di riconoscimento è in esecuzione. Utilizzando questo approccio, crei i tuoi metodi di listener e callback di riconoscimento. Per esempio: l'ascolto
inizio:
mSpeechRecognizer.startListening(mRecognizerIntent);
dove 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");
quindi, create il vostro ascoltatore:
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");
}
};
è possibile aggiungere le handleResults() a fare qualsiasi cosa tu vuoi.
Non è la prima volta che ho sentito parlare di CMUSphinx mentre stavo cercando. Grazie per l'info. – LefterisL