Page 164 - The Definitive Guide to Building Java Robots
P. 164
Preston_5564C05.fm Page 145 Tuesday, September 20, 2005 5:13 AM
CHAPTER 5 ■ SPEECH 145
Example 5-5. JavaVoice.java
package com.scottpreston.javarobot.chapter5;
import java.util.Locale;
import javax.speech.Central;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
public class JavaVoice implements JVoice {
private Synthesizer synth;
public JavaVoice() throws Exception {
// constructs synthesizer for US English
synth = Central.createSynthesizer(new SynthesizerModeDesc(
null, // engine name
"general", // mode name
Locale.US, // local
null, // Boolean, running
null)); // Voices[]
}
// allocates synthesizer resources, puts engine in state ALLOCATED.
public void open() {
try {
synth.allocate();
} catch (Exception e) {
e.printStackTrace();
}
}
// deallocates synthesizer resources, puts engine in state DEALLOCATED.
public void close() {
try {
synth.deallocate();
} catch (Exception e) {
e.printStackTrace();
}
}
// speaks
public void speak(String words) throws Exception {
// removes from paused state as set by allocate
synth.resume();
// speaks plain text and text is not interpreted by Java Speech Markup
// Language JSML
synth.speakPlainText(words, null);