我试图在我的 java 程序中使用 FreeTTS(来自https://freetts.sourceforge.io/docs/index.php),我收到了这个错误:java.lang.ClassCastException: com.sun.speech.freetts.en.us.cmu_time_awb.AlanVoiceDirectory cannot be cast to com.sun.speech.freetts.VoiceDirectory
我尝试将免费 TTS jar 文件重新添加到我的项目中,这是我的代码:
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
public class TextToSpeech {
//String voiceName = "kevin16";
VoiceManager freeVM;
Voice voice;
public TextToSpeech(String words) {
freeVM = VoiceManager.getInstance();
voice = VoiceManager.getInstance().getVoice("kevin16");
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
SpeakText(words);// Calling speak() method
} catch (Exception e1) {
e1.printStackTrace();
}
}
public void SpeakText(String words) {
voice.speak(words);
}
TextToSpeech
我像这样从另一个类调用构造函数:
new TextToSpeech("Hello World");
将不胜感激任何帮助或建议!