问题
I want display in a TextView what I say using the tts engine. I have a Button:
btnparla.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something");
try {
startActivityForResult(i, VOICE_REC);
//txt.setText("");
} catch (ActivityNotFoundException e){
Toast t = Toast.makeText(getApplicationContext(), "Errore", Toast.LENGTH_SHORT);
t.show();
}
}
});
and then:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO: Implement this method
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case VOICE_REC: {
if (resultCode == Activity.RESULT_OK) {
ArrayList<String> dico = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
resultList.setText(dico.get(0));
}
break;
}
}
}
where resultList is a TextView declared in the onCreate resultList = (TextView) findViewById(R.id.list);. The Button works but does not save anything in the TextView. It does not display what I say. What's wrong?
回答1:
The parameter VOICE_REC in startActivityForResult(i, VOICE_REC); is the requestCode not the resultCode. Change the switch condition from switch (resultCode) to switch (requestCode).
switch (requestCode) {
case VOICE_REC: {
if (resultCode == Activity.RESULT_OK) {
ArrayList<String> dico = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
resultList.setText(dico.get(0));
}
break;
}
}
来源:https://stackoverflow.com/questions/22299577/speech-recognition-not-works-android