Exception in thread "main" java.lang.ClassCastException: class com.sun.speech.freetts.en.us.cmu_time_awb.AlanVoiceDirectory

226 Views Asked by At

I am trying to develop a program that converts text to speech using freetts in java. I had already added all the required jar files and also build path.

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
public class TextToSpeech {

     public static void main(String[] args) {
    
    
         Voice voice;//Creating object of Voice class
         voice = VoiceManager.getInstance().getVoice("kevin");//Getting voice
         if (voice != null) {
             voice.allocate();//Allocating Voice
         }
         try {
             voice.setRate(190);//Setting the rate of the voice
             voice.setPitch(150);//Setting the Pitch of the voice
             voice.setVolume(3);//Setting the volume of the voice 
             voice.speak("Hello world");//Calling speak() method

         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
    

     }

 }

But facing an error.

Exception in thread "main" java.lang.ClassCastException: class com.sun.speech.freetts.en.us.cmu_time_awb.AlanVoiceDirectory
1

There are 1 best solutions below

0
wloleo On

Probably some jars were not linked correctly.. Started working as soon as I built the project using netbeans!

For those who might be having the same problem, use netbeans to avoid hassle of libraries.

  1. add lib/freetts.jar from "lib" folder in "freetts-1.2.2-src.zip" (downloadable from sf.net)
  2. add jdk folder (if not already listed) to project libraries
  3. You are done! now run the code.