首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修正“不能转换为com.sun.speech.freetts.VoiceDirectory”的错误?

如何修正“不能转换为com.sun.speech.freetts.VoiceDirectory”的错误?
EN

Stack Overflow用户
提问于 2018-10-26 12:08:40
回答 1查看 1.9K关注 0票数 2

我试图在我的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文件重新添加到我的项目中,这是我的代码:

代码语言:javascript
复制
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构造函数,如下所示:

代码语言:javascript
复制
 new TextToSpeech("Hello World");

如有任何帮助或建议,将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-26 12:51:10

像这样改变TextToSpeech结构:

代码语言:javascript
复制
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");
    }
}

这样做的目的是指示自由用户使用com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory类而不是AlanVoiceDirectory类。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53008424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档