1

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".

1
  • 1
    Try adding spaces (1 2 3 4) . It worked for me (and get used to always format the numbers for TTS). Oct 1, 2013 at 10:03

2 Answers 2

2

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;
}
2

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)

1
  • Did this work for any one?
    – bhkiran
    Jun 11, 2020 at 11:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.