0A01-APP-Text-to-Speech(二)

2014-11-24 12:54:58 · 作者: · 浏览: 10
bled(true); // Greet the user. sayHello(); } } else { // Initialization failed. Log.e(TAG, "Could not initialize TextToSpeech."); } } private static final Random RANDOM = new Random(); private static final String[] HELLOS = { "Hello", "Salutations", "Greetings", "Howdy", "What's crack-a-lackin ", "That explains the stench!" }; private 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); } }
实现步骤:

1.继承TextToSpeech.OnInitListener

2.实现onInit方法在这里加载语言int result = mTts.setLanguage(Locale.US);

3.初始化的实例TextToSpeech mTts = mTts = new TextToSpeech(this,
this // TextToSpeech.OnInitListener
);

4.调用其的speak方法private 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);
}