【问题标题】:Speak failed: TTS engine connection not fully set up说话失败:TTS 引擎连接未完全建立
【发布时间】:2018-07-06 11:18:32
【问题描述】:

我正在开发一个研究应用程序,其中手机使用语音从表单中读取用户问题(我使用 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();
    }
};

【问题讨论】:

  • 是disastersArray[] null ?? , 打勾
  • 不,已经满了。我这里只是省略了初始化。

标签: android speech-recognition google-text-to-speech android-speech-api


【解决方案1】:

你是这样调用 TTS 的:

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

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

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

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

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


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

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 2016-05-23
    • 2016-05-18
    • 2014-02-25
    • 2016-05-18
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多