How do we put on some Timers with the speech recognition?



  • A sequence of timers should be implemented, which will recognize speech and compare the results of recognition. The problem is, I can't do a few things. onActivityResultwhere the comparison will take place, in series, separately for each individual recognition. Consequently, all comparisons must occur in one. onActivityResult

    The question is, how do I change my mind? onActivityResult In accordance with my requirements?

    To compare each individual timer. If they're more than 10.

    public class MainActivity extends AppCompatActivity {
    

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Timer timer1 = new Timer();
    timer1.schedule(new TimerTask() {
    
        @Override
        public void run() {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!");
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
            startActivityForResult(intent, 1);
        }
    }, 0);
    

    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK) {
            ArrayList<String> results;
            results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            TextView speechText = (TextView)findViewById(R.id.textView1);
            String str="";
            for(int i=0;i<results.size();i++){
                str+= results.get(i);
            }
            if (str.equals("mercedes")) {
                rightAnswer();
            }else{
                speechText.setText("It's not: " + str);
            }
        }
    
        private void rightAnswer() {
    Timer timer2 = new Timer();
    timer2.schedule(new TimerTask() {
    
        @Override
        public void run() {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!");
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
            startActivityForResult(intent, 1);
        }
    }, 0);
    

    }

            }
    

    Implementation onActivityResult (2)

        @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case 1:
    if (requestCode == 1 && resultCode == RESULT_OK) {
    ArrayList<String> results;
    results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    TextView speechText = (TextView) findViewById(R.id.textView1);
    String str = "";
    for (int i = 0; i < results.size(); i++) {
    str += results.get(i);
    }
    if (str.equals("mercedes")) {
    speechText.setText(str);
    rightAnswer();
    } else {
    wrongAnswer();
    speechText.setText(str);
    }
    }
    case 2:
    if (requestCode == 1 && resultCode == RESULT_OK) {
    ArrayList<String> results;
    results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    TextView speechText = (TextView) findViewById(R.id.textView1);
    String str = "";
    for (int i = 0; i < results.size(); i++) {
    str += results.get(i);
    }
    if (str.equals("maserati")) {
    speechText.setText(str);
    Log.e("sec", "right");
    } else {
    speechText.setText(str);
    Log.e("sec", "wrong");
    }
    }
    }
    }



  • Bring in the timer identifier and send his identifier in the parameters. onActivityResult Enter the timer identifier.

    There's only one thing in the system. IntentI don't know what's going to happen if two timers call the same thing at the same time. Intent Specify speech. There's a need to synchronize the call. IntentOtherwise there'll be a pot.

    Update

    About:

    Timer timer=new Timer("1"); //задаем идентификатор таймера
            timer.schedule(new TimerTask() {
    
            @Override
            public void run() {
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!");
                int timerId=Integer.parseInt(Thread.currentThread().getName()); //получаем идентификатор таймера
                startActivityForResult(intent, timerId); //засылаем его в Intent
            }
        });
    

    Next, after the end of Intent's

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) { //это и есть идентификатор таймера
    //бла-бла
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2