0

我正在开发一个研究应用程序,其中手机使用语音从表单中读取用户问题(我使用 TextToSpeech 来读取问题),并且用户必须通过说话来填写表单,我正在使用 SpeechRecognizer 类。

我正在使用 UtteranceProgressListener 来检测电话何时停止通话,以便我可以启动 SpeechRecognizer。但每次,手机都会跳过应用程序中的第一个问题,只问最后一个问题。我不明白为什么会这样。我将不胜感激任何帮助或对此的见解。

最重要的是,我在我的日志中得到了这个,我什至不确定它是否与问题有关。

W/TextToSpeech:setLanguage 失败:TTS 引擎连接未完全建立

说话失败:TTS 引擎连接未完全建立

这就是代码:我写了两个 UtteranceProgressListeners,每个问题一个。

public class PastDisastersActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, RecognitionListener {

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

    params = new HashMap<>();
    progressBar = (ProgressBar) findViewById(R.id.speechProgressBar);

    progressBar.setVisibility(View.INVISIBLE);
    speech = SpeechRecognizer.createSpeechRecognizer(this);
    Log.i(LOG_TAG, "isRecognitionAvailable: " + SpeechRecognizer.isRecognitionAvailable(this));
    speech.setRecognitionListener(this);
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
            "en");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);

    handler = new Handler(getApplicationContext().getMainLooper());
    
    introduction();
    getVoiceInputForDisaster();
    getVoiceInputForTime();
    getVoiceInputForSeverity();
    for (String name: this.params.keySet()){
        Log.i(LOG_TAG,name);
    }

}

private void introduction() {
    final String toSpeak ="There are three questions in the new page. The questions are on "+           textView10.getText      () + "," + textView11.getText() + "," + textView12.getText();
    textToSpeech=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.UK);

                textToSpeech.setOnUtteranceProgressListener(mProgressListenerIntroduction);

                params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.valueOf(41));

                textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, params);

            }
        }
    });
}

private void getVoiceInputForDisaster() {
    final String toSpeak = "Which disaster do you want to update information about? There are four choices. They        are "+disastersArray[0]+", "+disastersArray[1]+", "+disastersArray[2]+" or "+disastersArray[3];
    textToSpeech=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.UK);

                textToSpeech.setOnUtteranceProgressListener(mProgressListenerDisaster);

                params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.valueOf(20));

                textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, params);

            }
        }
    });
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]       grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case REQUEST_RECORD_PERMISSION:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                speech.startListening(recognizerIntent);
            } else {
                Toast.makeText(PastDisastersActivity.this, "Permission Denied by Android!", Toast
                        .LENGTH_SHORT).show();
            }
    }
}

@Override
public void onResume() {
    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();

}

@Override
protected void onStop() {
    super.onStop();
    if (speech != null) {
        speech.destroy();
        Log.i(LOG_TAG, "destroy");
    }
}


@Override
public void onBeginningOfSpeech() {
    Log.i(LOG_TAG, "onBeginningOfSpeech");
    progressBar.setIndeterminate(false);
    progressBar.setMax(10);
}

@Override
public void onRmsChanged(float rmsdB) {

}

@Override
public void onBufferReceived(byte[] buffer) {
    Log.i(LOG_TAG, "onBufferReceived: " + buffer);
}

@Override
public void onEndOfSpeech() {
    Log.i(LOG_TAG, "onEndOfSpeech");
    progressBar.setIndeterminate(true);
}

@Override
public void onError(int errorCode) {
    String errorMessage = getErrorText(errorCode);
    Log.d(LOG_TAG, "FAILED " + errorMessage);
    Toast.makeText(PastDisastersActivity.this, errorMessage, Toast.LENGTH_LONG).show();
}

@Override
public void onEvent(int arg0, Bundle arg1) {
    Log.i(LOG_TAG, "onEvent");
}

@Override
public void onPartialResults(Bundle arg0) {
    Log.i(LOG_TAG, "onPartialResults");
}

@Override
public void onReadyForSpeech(Bundle arg0) {
    Log.i(LOG_TAG, "onReadyForSpeech");
}

@Override
public void onResults(Bundle results) {
    Log.i(LOG_TAG, "onResults");
    ArrayList<String> matches = results
            .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    text = "";
    for (String result : matches)
        text += result + "\n";

    //Processing

}

@Override
public void onPointerCaptureChanged(boolean hasCapture) {

}

private abstract class CustomRunnable implements Runnable {

}

private UtteranceProgressListener mProgressListenerIntroduction = new UtteranceProgressListener() {
    @Override
    public void onStart(String utteranceId) {
        //Toast.makeText(PastDisastersActivity.this,"In OnStart",Toast.LENGTH_LONG).show();
        handler.post(new CustomRunnable() {
            @Override
            public void run() {
                progressBar.setIndeterminate(false);
                progressBar.setVisibility(View.INVISIBLE);
                speech.stopListening();
            }
        });
    } // Do nothing

    @Override
    public void onError(String utteranceId) {
    } // Do nothing.

    @Override
    public void onDone(String utteranceId) {

        new Thread()
        {
            public void run()
            {
                handler.post(new CustomRunnable()
                {
                    public void run()
                    {
                        Toast.makeText(getBaseContext(), "TTS Completed", Toast.LENGTH_SHORT).show();
                        progressBar.setVisibility(View.VISIBLE);
                        progressBar.setIndeterminate(true);
                        ActivityCompat.requestPermissions
                                (PastDisastersActivity.this,
                                        new String[]{Manifest.permission.RECORD_AUDIO},
                                        REQUEST_RECORD_PERMISSION);


                    }
                });
            }
        }.start();
    }
};

private UtteranceProgressListener mProgressListenerDisaster = new UtteranceProgressListener() {
    @Override
    public void onStart(String utteranceId) {
        //Toast.makeText(PastDisastersActivity.this,"In OnStart",Toast.LENGTH_LONG).show();
        handler.post(new CustomRunnable() {
            @Override
            public void run() {
                progressBar.setIndeterminate(false);
                progressBar.setVisibility(View.INVISIBLE);
                speech.stopListening();
            }
        });

    } // Do nothing

    @Override
    public void onError(String utteranceId) {
    } // Do nothing.

    @Override
    public void onDone(String utteranceId) {

        new Thread()
        {
            public void run()
            {
                handler.post(new CustomRunnable()
                {
                    public void run()
                    {
                        Toast.makeText(getBaseContext(), "TTS Completed", Toast.LENGTH_LONG).show();
                        progressBar.setVisibility(View.VISIBLE);
                        progressBar.setIndeterminate(true);
                        ActivityCompat.requestPermissions
                                (PastDisastersActivity.this,
                                        new String[]{Manifest.permission.RECORD_AUDIO},
                                        REQUEST_RECORD_PERMISSION);

                    }
                });
            }
        }.start();
    }
};
4

1 回答 1

0

您以这种方式调用 TTS :

introduction();
getVoiceInputForDisaster();
getVoiceInputForTime();
getVoiceInputForSeverity();

但每次,手机都会跳过应用程序中的第一个问题,只问最后一个问题。

并且,每个方法都包含TTS 变量初始化代码( ttsVar = new TextToSpeech(...);) ttsvar.speak(...).

您不必等待一个方法调用完成并开始另一个方法调用,即使您也更改了TextToSpeech对象实例和文本。

这就是为什么之前的调用在开始之前就结束了!你正在让最后一个工作。


您必须自己一个一个地完成它。希望这会有所帮助。

于 2018-07-06T11:52:56.350 回答