简体   繁体   中英

Speech recognition not works Android

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?

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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM