Search for an array generated by sound recognition

I am working on an application that takes user audio and stores it in an arrayList and then loops through that list of arrays to see if the desired word generated by google API exists, so we can do actions. The action I wanted is removed and replaces it with a text preview to make sure it works correctly, the audio handler works correctly, but I always got the text of the text as a stop. So what's the problem?

package myfirstapp.myapps.me.voicerecognition;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    ListView lv;
    Button recBtn;
    static final int check = 1111;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.lv);
        recBtn = (Button) findViewById(R.id.recBtn);
        tv = (TextView) findViewById(R.id.textView);
        recBtn.setOnClickListener(MainActivity.this);
    }

    public void onClick(View v) {
        Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak");
        startActivityForResult(i, check);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == check && resultCode == RESULT_OK) {
            ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            lv.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, results));
            String s = "start";
            for (String string : results) {
//                if(string.matches("start"))
//                    tv.setText("Ok");
//                else
//                    tv.setText("No start");
//                }
                if (string.startsWith(s))
                    tv.setText("Start");
                else
                    tv.setText("Stop");
            }
            super.onActivityResult(requestCode, resultCode, data);

        }
    }

}

      

+3


source to share


1 answer


You don't exit the loop when you find a match, so if the match is not in the last line of the list, the text view will contain Stop at the end.

You can change it to:



       boolean match = false;
       for (String string : results) {
            if (string.startsWith(s)) {
                tv.setText("Start");
                match = true;
                break;
            }   
       }
       if (!match)
           tv.setText("Stop");

      

+2


source







All Articles