I have a requirement( in Android code) that TTS player should speak out numbers eg "1234" as "one two three four". However currently its speaking it as "one thousand two hundred and thirty four".

有帮助吗?

解决方案

TTS does not allow you to specify how the text should be read so your code has to modify the text input so that it reads the individual numbers.

I suggest @opalenzuela's mention that a space in between each number. That should cause the TextToSpeech to read the individual numbers.

If you need some code to help you detect numbers use this:

  private boolean isNumber(String word)
 {
boolean isNumber = false;
  try
  {
      Integer.parseInt(word);
      isNumber = true;
   } catch (NumberFormatException e)
  {
       isNumber = false;
  }
  return isNumber;
}

其他提示

When targeting API level 21+ (Lollipop), try the new possibilities of TtsSpan:

SpannableString spannableString = new SpannableString("The number is 123456789.");
spannableString.setSpan(new TtsSpan.DigitsBuilder().build(), 14, 22, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
new TextToSpeech(context, null).speak(spannableString, 0, null, null);

(not tested)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top