为了账号安全,请及时绑定邮箱和手机立即绑定

如何修复错误“无法投射到 com.sun.speech.freetts

如何修复错误“无法投射到 com.sun.speech.freetts

繁花不似锦 2021-12-10 10:20:59
我试图在我的 Java 程序(来自https://freetts.sourceforge.io/docs/index.php)中使用 FreeTTS并且我收到此错误: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");将不胜感激任何帮助或建议!
查看完整描述

2 回答

?
月关宝盒

TA贡献1772条经验 获得超5个赞

像这样更改 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类。


查看完整回答
反对 回复 2021-12-10
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

代码更改:


而不是以下行


freeVM = VoiceManager.getInstance();

voice = VoiceManager.getInstance().getVoice("kevin16");

修改为


freeVM = VoiceManager.getInstance();

voice = freeVM.getVoice("kevin16");


查看完整回答
反对 回复 2021-12-10
  • 2 回答
  • 0 关注
  • 216 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信