开发者

String Array increment each string in TextToSpeech

Using the text to speech API I want to change the string array to increment from index 0 string and when it reaches the end it will go back to the beginning.

At the moment it uses a random generator and the method works as follows:

 public static void sayHello() {
    // Select a random h开发者_运维问答ello.

    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 is the array and it holds the strings:

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

Appreciate any help Thanks.


When you want to increment an index but loop around to zero again modulo is your friend.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜