001package ca.bc.webarts.tools; 002 003 004import java.util.Locale; 005 006import javax.speech.Central; 007import javax.speech.synthesis.Synthesizer; 008import javax.speech.synthesis.SynthesizerModeDesc; 009import javax.speech.synthesis.SynthesizerProperties; 010 011 012public class TextSpeech 013{ 014 015 public static void main(String[] args) 016 { 017 018 try 019 { 020 021 // Set property as Kevin Dictionary 022 System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us" + ".cmu_us_kal.KevinVoiceDirectory"); 023 024 // Register Engine 025 Central.registerEngineCentral("com.sun.speech.freetts" + ".jsapi.FreeTTSEngineCentral"); 026 027 // Create a Synthesizer 028 Synthesizer synthesizer = Central.createSynthesizer( 029 new SynthesizerModeDesc(Locale.US)); 030 031 // Allocate synthesizer 032 synthesizer.allocate(); 033 034 // Resume Synthesizer 035 synthesizer.resume(); 036 037 // Speaks the given text 038 // until the queue is empty. 039 040 SynthesizerProperties synthProps = synthesizer.getSynthesizerProperties(); 041 System.out.println("Current Voice name="+synthProps.getVoice().getName()); 042 synthProps.setVoice("kevin16"); 043 System.out.println(" NEW Voice name="+synthProps.getVoice().getName()); 044 synthesizer.speakPlainText("Ok Google, how can I help you?", null); 045 synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY); 046 047 // Deallocate the Synthesizer. 048 synthesizer.deallocate(); 049 } 050 catch (Exception e) 051 { 052 e.printStackTrace(); 053 } 054 } 055} 056