Как исправить ошибку "невозможно привести к com.sun.speech.freetts.VoiceDirectory"?
Я пытаюсь использовать FreeTTS в моей программе Java (из 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
Я попытался повторно добавить файлы JAR Free TTS в мой проект, и это мой код:
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 ответа
Измените конструктор 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");
}
}
Идея состоит в том, чтобы инструктировать com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory
класс вместо AlanVoiceDirectory
учебный класс.
Изменения кода:
вместо следующей строки
freeVM = VoiceManager.getInstance();
voice = VoiceManager.getInstance().getVoice("kevin16");
Изменить на
freeVM = VoiceManager.getInstance();
voice = freeVM.getVoice("kevin16");
См. Пример базы кода, приведенной здесь: FreeTTS не может найти голос