"Hello World" program for the Java Speech Synthesis API
import javax.speech.*;
import javax.speech.synthesis.*;
import java.util.Locale;
public class HelloWorld {
public static void main(String args[]) {
try
{
// Create a synthesizer for English
Synthesizer synth = Central.createSynthesizer(
new SynthesizerModeDesc(Locale.ENGLISH));
// Get it ready to speak
synth.allocate();
synth.resume();
// Speak the "Hello world" string
synth.speakPlainText("Hello, world!", null);
// Wait till speaking is done
synth.waitEngineState(Synthesizer.QUEUE_EMPTY);
// Clean up
synth.deallocate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
This example illustrates the four basic steps which all
speech synthesis applications must perform. Let's examine
each step in detail.
Create: The Central class of javax.speech package is used to obtain a speech synthesizer by calling the
createSynthesizer method. The SynthesizerModeDesc argument provides the information needed to
locate an appropriate synthesizer. In this example a synthesizer that speaks English is requested.
Allocate and Resume: The allocate and resume methods prepare the Synthesizer to produce speech
by allocating all required resources and putting it in the RESUMED state.
Generate: The speakPlainText method requests the generation of synthesized speech from a string.
Deallocate: The waitEngineState method blocks the caller until the Synthesizer is in the
QUEUE_EMPTY state - until it has finished speaking the text. The deallocate method frees the synthesizer's
resources.
|