gpt4 book ai didi

java - 如何修复错误 "cannot be cast to com.sun.speech.freetts.VoiceDirectory"?

转载 作者:行者123 更新时间:2023-11-30 02:00:30 33 4
gpt4 key购买 nike

我尝试在我的java程序中使用FreeTTS(来自 https://freetts.sourceforge.io/docs/index.php ),但收到此错误: java.lang.ClassCastException: com.sun.speech.freetts.en.us.cmu_time_awb.AlanVoiceDirectory 不能转换为 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");

非常感谢任何帮助或建议!

最佳答案

像这样更改 TextToSpeech 构造函数:

public TextToSpeech(String words) {
System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
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();
}

} else {
throw new IllegalStateException("Cannot find voice: kevin16");
}
}

这个想法是指示 freetts 使用 com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory 类而不是 AlanVoiceDirectory 类。

关于java - 如何修复错误 "cannot be cast to com.sun.speech.freetts.VoiceDirectory"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53008424/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com