我正在努力让一个TTS在java的netbeans中工作。我解压缩了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)**
有谁知道如何才能让它发挥作用?
我只是将字符串转换为语音的程序。
答案 0 :(得分:0)
错误#1:您必须将一些参数传递给main方法才能使用args[0]
。有关其他信息,请参阅What is "String args[]"? parameter in main method Java。
错误#2:您在代码中遇到NullPointerException
,这意味着您尝试在等于null
的实例上使用方法。要么使用调试器,要么使用e.printStackTrace();
而不是System.out.println (""+e);
扩展调试信息,以便知道哪个实例是罪魁祸首。