Problem of the implementation of the Timer sequence



  • The annex shall be downloaded MediaPlayerThen Vice recognitionIf the voice is equal.equals) word provided ifthat word is put in TextView and loaded Voice recognition♪ If it doesn't matter, the wrong word is put. TextView and downloaded MediaPlayer

    I've got a problem with the method. onActivityResult conditions switch implemented case 1but not implemented case 2Voice recognitionbut he won't. I can't figure out what the problem is.

    public class MainActivity extends AppCompatActivity {
    

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

    Timer timer1 = new Timer();
    timer1.schedule(new TimerTask() {
    
        @Override
        public void run() {
            MediaPlayer voice = MediaPlayer.create(MainActivity.this, R.raw.carMercedes);
            voice.start();
            voice.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer voice) {
                    voice.release();
                    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!");
                            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
                            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
                            int timerId=Integer.parseInt(Thread.currentThread().getName());
                            startActivityForResult(intent, timerId);
                        }
                    }, 0);
                }
            });
        }
    }, 0);
    

    }
    @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")) {
    rightAnswer();
    speechText.setText(str);
    } 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);
    } else {
    speechText.setText(str);
    }
    }
    }
    }

    private void wrongAnswer() {
    MediaPlayer voice = MediaPlayer.create(MainActivity.this, R.raw.carMaserati);
    voice.start();
    voice.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer voice) {
    voice.release();
    }
    });
    }
    private void rightAnswer() {
    Timer timer = new Timer("2");
    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!");
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
            int timerId=Integer.parseInt(Thread.currentThread().getName());//получаем идентификатор таймера
            startActivityForResult(intent, timerId);//засылаем его в Intent
        }
    }, 0);
    

    }
    }



  • (1) After case I need you. break

    (2) Why are you checking? requestCode == 1 If you already have case 1: It's worth it.




Suggested Topics

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