我正在开发一个需要文本到语音的应用程序。因此,我阅读了一些tts教程并做到了:
public void buttonClick (View view) {
TextToSpeech tts = new TextToSpeech (this, new TextToSpeech.OnInitListener () {
@Override
public void onInit(int status) {
}
});
tts.setLanguage (Locale.UK);
tts.speak ("Hello World", TextToSpeech.QUEUE_FLUSH, null);
}
但是,当我运行该应用程序并单击该按钮时,听不到声音,只有按钮的“喀哒”声。这也意味着扬声器已打开。
我猜这是因为
speak(String, int, HashMap<String, String>)
已过时。当我查看文档时,这种猜测被证明是错误的: /** @deprecated As of API level 21, replaced by
* {@link #speak(CharSequence, int, Bundle, String)}.
*/
我的应用程序的最低SDK版本为API18,而设备的Android版本为Android 4.3。这意味着不建议使用
speak
。弃用可能只是Android Studio的一个错误。我不知道为什么它没有声音,如何解决这个问题。
最佳答案
public void buttonClick (View view) {
TextToSpeech tts = new TextToSpeech (this, new TextToSpeech.OnInitListener () {
@Override
public void onInit(int status) {
// change required.Initialization has to finish first.
if(status != TextToSpeech.ERROR) {
tts.setLanguage (Locale.UK);
tts.speak ("Hello World", TextToSpeech.QUEUE_FLUSH, null);
}
}
});
}