我正在尝试让 TTS 在 netbeans for java 中工作。我已解压缩 JSAPI 文件并将以下 jars 添加到库中
- cmu_us_kal.jar
- cmulex.jar
- en_us.jar
- freetts.jar
- cmulex.jar
- jsapi.jar
当我运行以下代码时:
package demofreetts2;
import javax.speech.*;
import java.util.*;
import javax.speech.synthesis.*;
public class Demofreetts2 {
String speaktext ="";
public void doSpeak(String speak, String voicename)
{
speaktext = speak;
String voiceName = voicename;
try
{
SynthesizerModeDesc desc = new SynthesizerModeDesc(null, "general", Locale.US, null, null);
Synthesizer synthesizer = Central.createSynthesizer(desc);
synthesizer.allocate();
synthesizer.resume();
desc = (SynthesizerModeDesc)synthesizer.getEngineModeDesc();
Voice[] voices = desc.getVoices();
Voice voice = null;
for(int i = 0; i< voices. length; i++)
{
if(voices[i].getName().equals(voiceName))
{
voice = voices[i];
break;
}
}
synthesizer.getSynthesizerProperties().setVoice(voice);
synthesizer.speakPlainText(speaktext, null);
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
synthesizer.deallocate();
}
catch (Exception e)
{
String message = "missing speech properties in " + System.getProperty("user.home") + "\n";
System.out.println (""+e);
}
}
public static void main (String[]args)
{
Demofreetts2 obj = new Demofreetts2();
obj.doSpeak(args[0],"kevin16");
}
}
我收到以下消息:
**run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at demofreetts2.Demofreetts2.main(Demofreetts2.java:46)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)**
当我像这样更改主要方法时:
public static void main (String[]args)
{
Demofreetts2 obj = new Demofreetts2();
obj.doSpeak("hello","kevin16");
}
我收到这条消息:
**run:
java.lang.NullPointerException
BUILD SUCCESSFUL (total time: 1 second)**
有谁知道我怎样才能让它工作?
我只是一个将字符串转换为语音的程序。