我正在尝试在服务中运行Text to Speech,而单个字符串运行完美(total = quantity * price - quantity * price * (discount / 100.0);
),当我尝试使用for循环运行它时,它不工作。任何帮助将不胜感激。
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null,null);
答案 0 :(得分:0)
实现 TextToSpeech.OnInitListener
并在 onInit
函数内部执行如下操作:
override fun onInit(status: Int) = if (status == TextToSpeech.SUCCESS) {
val result: Int = textToSpeech.setLanguage(Locale.getDefault())
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED
) {
Toast.makeText(applicationContext, "Language not supported", Toast.LENGTH_SHORT).show()
}
// Wait 2 seconds to tell the first name
// Add some silence between the names
val arrayOfStrings = arrayOf(
caller_name_label.text.toString(),
caller_name_label.text.toString(),
caller_name_label.text.toString()
)
var i = 0
for (name in arrayOfStrings) {
lifecycleScope.launch {
delay(2000L)
textToSpeech.speak(arrayOfStrings[i], TextToSpeech.QUEUE_ADD, null, null)
textToSpeech.playSilentUtterance(2000L, TextToSpeech.QUEUE_ADD, null);
i += 1
}
}
} else {
Toast.makeText(applicationContext, "Init failed", Toast.LENGTH_SHORT).show()
}
答案 1 :(得分:-1)
使用它并让我知道
TextToSpeech tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.US);
for (String a :arr)
{
tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null);
}