1

初学者问题...

我有以下 4 行代码。

第三行询问用户(通过语音)他/她是否确定某个动作。

然后第四行调用一个名为 SpeechYesNo 的方法(包括在下面)来监听用户的响应。

问题是在 txtSpeech.speak 行(第 3 行)说完之前调用了 speechYesNo 方法,它记录了“Are you sure...”语音行以及用户的响应。

例如,如果用户回答“是”,speechYesNo 方法的结果将类似于...“您确定要添加 xxxxx 是吗”。

我只希望结果是“是”。

有没有办法在调用 SpeechYesNo() 方法之前强制第三行(txtSpeech.speak)完成?

谢谢

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

speechYesNo();

这是 SpeechYesNo() 方法

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();
    }

}

这是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;
    }
}

这是完整的脚本

openSpeechMode 方法将在用户单击图像时触发并记录用户的语音选择。

完成此操作后,我希望应用程序询问用户他/她是否确定他/她的选择。

如果用户回答“是”,那么我会做更多的工作。

上述问题是语音记录器正在记录“你确定”问题以及“是”响应。

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;
    }

}
4

1 回答 1

1

问题

问题是该speechYesNo方法在 txtSpeech.speak

解决方案

txtSpeech似乎是TextToSpeech类。因此,您应该使用UtteranceProgressListener来解决您的问题。

试试下面的代码。

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);
于 2020-02-11T06:47:01.160 回答