سؤال

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