Android Text To Speech And Numbers

2019-01-26 22:30发布

问题:

I am using Android Text to Speech engine for reading some text and it's working. But my text contains numbers and I want the numbers to be read digit by digit.

I couldn't find anything in the documentation, but I am still hoping someone knows how I can do that.

回答1:

The API 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 adding 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:

The accepted answer has a minor flaw . If the number has '0' as one of it's digits , it would be read as alphabet 'o' instead of Zero . I would suggest the following solution .

 String number = "1230";

 for(int i = 0 ; i < number.size(); i++) {

  /* refer Speech API , Don't use QUEUE_FLUSH as it results in flushing      
     some characters in this case */

   engine.speak(Character.toString(number.charAt(i)),QUEUE_ADD,null);

  }


回答3:

/* refer Speech API , Don't use QUEUE_FLUSH as it results in flushing      
some characters in this case */
for(int i = 0 ; i < number.size(); i++) {
  engine.speak(Character.toString(number.charAt(i)),QUEUE_ADD,null); 
}