2011-02-19 103 views
1

使用文本到语音API我想更改字符串数组以便从索引0字符串增加,当它到达结尾时将返回到开始处。字符串数组增加TextToSpeech中的每个字符串

目前,它采用的是随机数发生器和方法的工作原理如下:

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

打着招呼是数组,并将其持有的字符串:

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

感谢所有帮助 感谢。

回答

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

@Raj:如果要在会话间记住这个,你必须保存currentHelloIndex的值,例如,使用[SharedPreferences](http://developer.android.com/reference/android/content/SharedPreferences.html) – Arve 2011-02-19 00:36:15

相关问题