Android Text To Speech And Numbers

守給你的承諾、 提交于 2019-12-18 07:01:00

问题


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


来源:https://stackoverflow.com/questions/11238517/android-text-to-speech-and-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!