0

I am trying to use the Speech recognition without dialog with RecognitionListener, but it doesn't work. The following codes are the way now I use, and the bottom codes (test part) are for checking that could phone receive my commanding. Additionally, I have added permissions Audio. Finally, I don't want there have any button.

This is my first time asking question, so hope I dont violate any unspoken rules.

Thank you very much,

public class Step extends AppCompatActivity {

private final int SPEECH_RECOGNITION_CODE = 1;
private TextView txtOutput;
private Button btnMicrophone;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_step);
    txtOutput = (TextView) findViewById(R.id.txt_output);
    btnMicrophone = (Button) findViewById(R.id.btn_mic);
    startSpeechToText();

    btnMicrophone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startSpeechToText();
        }
    });
}



private void startSpeechToText() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "SPEAK");
    try {
        startActivityForResult(intent, SPEECH_RECOGNITION_CODE);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(), "Sorry! Speech recognition is not supported in this device.", Toast.LENGTH_SHORT).show();
    }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case SPEECH_RECOGNITION_CODE: {
            if (resultCode == RESULT_OK && null != data) {
                ArrayList<String> result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                String text = result.get(0);
                txtOutput.setText(text);
                test(txtOutput.getText().toString());
                startSpeechToText();
            }
            break;
        }
    }
}



public void test (String com) {
    for(int i=0;i<com.length();i++) {
        if (com.startsWith("good",i)) {
            com = com + "123";
            Toast.makeText(CookStep.this, com, Toast.LENGTH_SHORT).show();
        }
    }
    if(com.compareToIgnoreCase("OK")==0){
        com=com+"456";
        Toast.makeText(CookStep.this, com, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(CookStep.this, com, Toast.LENGTH_SHORT).show();

    }

}

}

Jay
  • 1
  • 2

0 Answers0