1

我得到空指针异常,我也知道原因。这是因为 synth 正在获取空值。我应该怎么做才能删除它?请帮忙

public class HelloWorld {

    public static void main(String args[]) {
        try {
           // Create a synthesizer for English
           Synthesizer synth = Central.createSynthesizer(new SynthesizerModeDesc(Locale.ENGLISH));

           // Get it ready to speak
           synth.allocate();

           synth.resume();

           // Speak the "Hello world" string

           synth.speakPlainText("Intiallizing the components, we are online and ready,sir", null);

          // Wait till speaking is done

          synth.waitEngineState(Synthesizer.QUEUE_EMPTY);

         // Clean up

         synth.deallocate();

    } catch (Exception e) {

         e.printStackTrace();

    }

}
4

3 回答 3

1

即使在包含所有 jar 文件之后,在您的代码中,您也应该为字典设置属性并设置引擎。您可以通过在 try 中包含此代码块来做到这一点:

System.setProperty("freetts.voices","com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
       Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
于 2018-10-25T18:10:58.597 回答
0

添加null检查以避免NullPointerException

if(synth != null)
{

synth.allocate();
synth.resume();
synth.speakPlainText("Intiallizing the components, we are online and ready,sir", null);
synth.waitEngineState(Synthesizer.QUEUE_EMPTY);
synth.deallocate();

}
于 2014-02-13T06:48:32.967 回答
-1

我想问题是你没有添加语音合成器。下载 FreeTTS(https://sourceforge.net/projects/freetts/)。将 zip 文件解压缩到一个文件夹中。在该文件夹中,您将看到 jsapi.exe。运行。它将创建 jsapi.jar。将所有 jar 文件添加到您的类路径中。然后试试下面的例子。

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

public class Text_to_speech {

/**
 * @param args the command line arguments
 */
    public static void main(String[] args) {

    String text = "Voice check!";

    Voice voice;
    VoiceManager voiceManager = VoiceManager.getInstance();
    voice = voiceManager.getVoice("kevin");
    voice.allocate();
    voice.speak(text);

 }


}

`

于 2017-01-21T13:27:29.393 回答