1

Beginner question...

I have the following 4 lines of code.

The third line asks the user (with speech) whether he / she is certain about an action.

Then the fourth line calls a method called speechYesNo (included below) to listen for the users response.

The problem is that the speechYesNo method is called before the txtSpeech.speak line (3rd line) is finished speaking and it records the "Are you sure... " speech line as well as the users response.

For example, if the user responds "Yes", the result from the speechYesNo method will be something like... "Are you sure you would like to add xxxxx Yes".

I just want the result to be "yes".

Is there a way to force the third line (txtSpeech.speak) to finish before the speechYesNo() method is called?

Thanks

String strCommonName = myUtil.getCommonName(strFinalResult);
String strToSpeak = "Are you sure you would like to add " + strCommonName;
txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null);

speechYesNo();

Here is the speechYesNo() method

public void speechYesNo() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

    if(intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, 9);
    } else {
        Toast.makeText(context, "Your device does not support Speech Input.", Toast.LENGTH_LONG).show();
    }

}

Here is the onActivityResult...

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

    switch (requestCode) {

        case 10:

            if(resultCode == RESULT_OK && data != null) {

               ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
               String strResult = result.get(0);
               Double dblStringComparison = 0.000;
               String strFinalResult = "Unknown";

               for(int intCount = 0; intCount <= arrAnimals.size()-1; intCount++) {

                   String strVal1 = strResult;
                   String strVal2 = myUtil.getCommonName(arrAnimals.get(intCount));
                   Double dblTemp = myUtil.similarity(strVal1, strVal2 );

                   if(dblTemp > dblStringComparison){
                       dblStringComparison = dblTemp;
                       strFinalResult = arrAnimals.get(intCount);
                   }

               }


                atvAnimalName.setText(strFinalResult);

                String strCommonName = myUtil.getCommonName(strFinalResult);
                String strToSpeak = "Are you sure you would like to add " + strCommonName;
                txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null);

                speechYesNo();

            }

            break;

        case 9:

            if(resultCode == RESULT_OK && data != null) {

                ArrayList<String> result2 = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                String strYesNo0 = result2.get(0);
                Toast.makeText(context, strYesNo0, Toast.LENGTH_LONG).show();

            }

            break;
    }
}

Here is the full script

The openSpeechMode method will fire when user clicks an image and will record the users speech selection.

Once this is done I want the app to ask the user is he / she is sure about his / her selection.

If the user responds "yes" then I will do some more work.

The problem as per above is the the speech recorder is recording the "are you sure " question as well as the "yes" response.

public void openSpeechMode(View view) {

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

    if(intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, 10);
    } else {
        Toast.makeText(context, "Your device does not support Speech Input.", Toast.LENGTH_LONG).show();
    }

}



public void speechYesNo() {

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

    if(intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, 9);
    } else {
        Toast.makeText(context, "Your device does not support Speech Input.", Toast.LENGTH_LONG).show();
    }

}



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

    switch (requestCode) {

        case 10:

            if(resultCode == RESULT_OK && data != null) {

               ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
               String strResult = result.get(0);
               Double dblStringComparison = 0.000;
               String strFinalResult = "Unknown";

               for(int intCount = 0; intCount <= arrAnimals.size()-1; intCount++) {

                   String strVal1 = strResult;
                   String strVal2 = myUtil.getCommonName(arrAnimals.get(intCount));
                   Double dblTemp = myUtil.similarity(strVal1, strVal2 );

                   if(dblTemp > dblStringComparison){
                       dblStringComparison = dblTemp;
                       strFinalResult = arrAnimals.get(intCount);
                   }

               }


                atvAnimalName.setText(strFinalResult);

                String strCommonName = myUtil.getCommonName(strFinalResult);
                String strToSpeak = "Are you sure you would like to add " + strCommonName;
                txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null);

//                    speechYesNo();

            }

            break;

        case 9:

            if(resultCode == RESULT_OK && data != null) {

                ArrayList<String> result2 = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                String strYesNo0 = result2.get(0);
                Toast.makeText(context, strYesNo0, Toast.LENGTH_LONG).show();

            }

            break;
    }

}
Ethan Choi
  • 2,339
  • 18
  • 28
Jon
  • 55
  • 2
  • 15

1 Answers1

1

Question

The problem is that the speechYesNo method is called before the txtSpeech.speak

Solution

txtSpeech seems to be TextToSpeech class. thus, you should use UtteranceProgressListener for resolving your problem.

Try as below code.

txtSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {
        }

        @Override
        public void onDone(String utteranceId) {
            speechYesNo();
        }

        @Override
        public void onError(String utteranceId) {
        }
    });
txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null);
Penguin74
  • 480
  • 6
  • 19
Ethan Choi
  • 2,339
  • 18
  • 28
  • txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null); we can provide UtteranceId for specific speak task. txtSpeech.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, null, "utteranceid"); – kuber singh Feb 11 '20 at 06:51