String Array增加TextToSpeech中的每个字符串

时间:2011-02-19 00:29:32

标签: android

使用文本到语音API我想将字符串数组更改为从索引0字符串递增,当它到达结尾时,它将返回到开头。

目前它使用随机生成器,方法如下:

 public static void sayHello() {
    // Select a random hello.

    int helloLength = HELLOS.length;
    String hello = HELLOS[RANDOM.nextInt(helloLength)];
    mTts.speak(hello,
        TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.
        null);
}

HELLOS是数组,它包含字符串:

 String [] HELLOS = {"One" "Two" "Three" "Four"};

感谢任何帮助 感谢。

1 个答案:

答案 0 :(得分:1)

如果你想增加一个索引但是再次循环到零,modulo就是你的朋友。

int currentHelloIndex = 0;
public static void sayHello() {
    // Select a random hello.

    int helloLength = HELLOS.length;
    String hello = HELLOS[currentHelloIndex];
    currentHelloIndex = (currentHelloIndex + 1) % helloLength;
    mTts.speak(hello,
        TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.
        null);
}
相关问题