Speech recognition not works Android

我的未来我决定 提交于 2020-01-06 13:17:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!