我正在开发一个研究应用程序,其中电话使用语音从表单中读取用户的问题(我正在使用TextToSpeech来阅读问题),并且用户必须通过讲话来填写表单,而我正在使用SpeechRecognizer进行交谈类。
我正在使用UtteranceProgressListener来检测电话何时停止通话,以便可以启动SpeechRecognizer。但是每次,电话都会跳过应用程序中的第一个问题,而只会询问最后一个问题。我不明白为什么会这样。我将不胜感激,对此深表感谢。
最重要的是,我在日志中得到了这个,我什至不确定与该问题有关。
W / TextToSpeech:setLanguage失败:TTS引擎连接未完全建立
说失败:TTS引擎连接未完全建立
这是代码: 我写了两个UtteranceProgressListener,每个问题一个。
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();
}
};
答案 0 :(得分:0)
您以这种方式致电TTS:
introduction();
getVoiceInputForDisaster();
getVoiceInputForTime();
getVoiceInputForSeverity();
但是每次手机都跳过应用程序中的第一个问题,而仅询问最后一个问题。
而且,每个方法都包含 TTS变量初始化代码(ttsVar = new TextToSpeech(...);
)和 ttsvar.speak(...)
。
即使您也更改了TextToSpeech
对象实例和文本,也不必等待一个方法调用完成并启动另一个方法。
这就是为什么先前的呼叫在开始之前就结束了的原因!并且您正在获得最后一个。
您必须自己一步一步地管理它。希望这会有所帮助。