Android 讲话失败:TTS引擎连接未完全设置

Android 讲话失败:TTS引擎连接未完全设置,android,speech-recognition,google-text-to-speech,android-speech-api,Android,Speech Recognition,Google Text To Speech,Android Speech Api,我正在开发一个研究应用程序,在该应用程序中,手机使用语音(我使用TextToSpeech来阅读问题)从表格中读取用户的问题,用户必须通过对话来填写表格,而我使用SpeechRecognitor类来完成这项工作 我正在使用OuttenceProgressListener检测手机何时停止通话,以便启动SpeechRecognitor。但每次,手机都会跳过应用程序中的第一个问题,只问最后一个问题。我不明白为什么会这样。如果有任何帮助或见解,我将不胜感激 最重要的是,我在我的日志中得到了这个,我甚至不确

我正在开发一个研究应用程序,在该应用程序中,手机使用语音(我使用TextToSpeech来阅读问题)从表格中读取用户的问题,用户必须通过对话来填写表格,而我使用SpeechRecognitor类来完成这项工作

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

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

W/TextToSpeech:setLanguage失败:TTS引擎连接未完全设置

讲话失败:TTS引擎连接未完全设置

这是代码: 我写了两篇演讲稿,每个问题一篇

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();
    }
};
公共类PastDisastersActivity扩展AppCompativeActivity实现AdapterView.OnItemSelectedListener、RecognitionListener{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u过去的灾难);
params=新的HashMap();
progressBar=(progressBar)findViewById(R.id.speechProgressBar);
progressBar.setVisibility(View.INVISIBLE);
SpeechRecognizer.createSpeechRecognizer(此);
Log.i(Log_标签,“isRecognitionAvailable:+SpeechRecognizer.isRecognitionAvailable(this));
speech.setRecognitionListener(此);
recognizerIntent=新意图(recognizerIntent.ACTION\u recognizer\u SPEECH);
recognizerIntent.putExtra(recognizerIntent.EXTRA_语言_首选项,
“en”);
recognizerIntent.putExtra(recognizerIntent.EXTRA_语言_模型,
识别者意图、语言、模型、自由形式);
recognizerIntent.putExtra(recognizerIntent.EXTRA_最大_结果,3);
handler=新处理程序(getApplicationContext().getMainLooper());
导言();
getVoiceInputForDisaster();
getVoiceInputForTime();
getVoiceInputForSeverity();
for(字符串名称:this.params.keySet()){
Log.i(日志标签、名称);
}
}
私人机构简介({
final String toSpeak=“新页面中有三个问题。这些问题位于“+textView10.getText()+”、“+textView11.getText()+”、“+textView12.getText()”;
textToSpeech=new textToSpeech(getApplicationContext(),new textToSpeech.OnInitListener()){
@凌驾
公共无效onInit(int状态){
if(状态!=TextToSpeech.ERROR){
textToSpeech.setLanguage(Locale.UK);
textToSpeech.setOnPatternanceProgressListener(mProgressListenerIntroduction);
参数put(TextToSpeech.Engine.KEY_参数_话语_ID,String.valueOf(41));
textToSpeech.speak(toSpeak,textToSpeech.QUEUE_FLUSH,params);
}
}
});
}
私有void getVoiceInputForDisaster(){
final String toSpeak=“您想更新关于哪个灾难的信息?有四种选择。它们是“+disastersArray[0]+”、“+disastersArray[1]+”、“+disastersArray[2]+”或“+disastersArray[3]”;
textToSpeech=new textToSpeech(getApplicationContext(),new textToSpeech.OnInitListener()){
@凌驾
公共无效onInit(int状态){
if(状态!=TextToSpeech.ERROR){
textToSpeech.setLanguage(Locale.UK);
textToSpeech.setOnPatternanceProgressListener(mProgressListenerDisaster);
参数put(TextToSpeech.Engine.KEY_参数_话语_ID,String.valueOf(20));
textToSpeech.speak(toSpeak,textToSpeech.QUEUE_FLUSH,params);
}
}
});
}
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
super.onRequestPermissionsResult(请求代码、权限、授权结果);
开关(请求代码){
案例请求\记录\许可:
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION\u已授予){
演讲。听讲(识别意图);
}否则{
Toast.makeText(PastDisastersActivity.this,“Android拒绝许可!”,Toast
.LENGTH_SHORT).show();
}
}
}
@凌驾
恢复时公开作废(){
super.onResume();
}
@凌驾
受保护的void onPause(){
super.onPause();
}
@凌驾
受保护的void onStop(){
super.onStop();
if(语音!=null){
演讲。破坏();
Log.i(Log_标签,“销毁”);
}
}
@凌驾
开始时的公共无效fSpeech(){
Log.i(Log_标签,“onbeginingofspeech”);
progressBar.SetUndeterminate(假);
progressBar.setMax(10);
}
@凌驾
在RMSCHANGED上的公共无效(浮动rmsdB){
}
@凌驾
已接收公共无效onBufferReceived(字节[]缓冲区){
Log.i(Log_标签,“onBufferReceived:”+buffer);
}
@凌驾
公共无效onEndOfSpeech(){
Log.i(Log_标签,“onEndOfSpeech”);
progressBar.SetUndeterminate(真);
}
@凌驾
公共无效onError(内部错误代码){
字符串errorMessage=getErrorText(errorCode);
Log.d(日志标签,“失败”+错误消息);
Toast.makeText(PastDisastersActivity.this、errorMessage、Toast.LENGTH_LONG.show();
}
@凌驾
public void onEvent(int arg0,Bundle arg1){
Log.i(Log_标签,“onEvent”);
}
@凌驾
public void on PartialResults(捆绑包arg0){
Log.i(Log_标签,“onPartialResults”);
}
@凌驾
ReadyForSpeech上的公共无效(捆绑arg0){
Log.i(Log_标签,“onReadyForSpeech”);
}
@凌驾
公共结果(捆绑结果){
Log.i(Log_标签,“onResults”);
ArrayList匹配=结果
.getStringArrayList(SpeechRecognitor.RESULTS_RECOGNITION);
text=“”;
for(字符串结果:匹配项)
文本+=结果+“\n”;
//加工
}
@凌驾
onPointerCaptureChanged上的公共无效(布尔值
introduction();
getVoiceInputForDisaster();
getVoiceInputForTime();
getVoiceInputForSeverity();