Freetts 无法使用外部扬声器

2024-04-04

我在我的 java 应用程序中使用了 Freetts.jar 文件来公布令牌编号。我的应用程序在我的笔记本电脑中运行良好,但在具有外部扬声器的台式机中无法运行。我得到一个空指针异常。注意:我的两台计算机都使用 Windows 7。

下面的代码是我使用的示例格式。

package tsapp;

import java.util.Locale;
import javax.speech.Central;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.swing.JOptionPane;
public class TextSpeech {
 public static void main(String[] args){
 try
 {
   System.setProperty("freetts.voices",
    "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");

   Central.registerEngineCentral
    ("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
   Synthesizer  synthesizer =
    Central.createSynthesizer(new SynthesizerModeDesc(Locale.US));
   synthesizer.allocate();
   synthesizer.resume();
   String str;

        str=JOptionPane.showInputDialog(null,"Voice Check");
        if(str==null)
        return;
        synthesizer.speakPlainText(str, null);
   synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
   synthesizer.deallocate();
  }
   catch(Exception e)
   {
       System.out.println(e.getClass());
     e.printStackTrace();
   }
 }
}

我们可以做一件简单的事情吗:

  1. 下载二进制文件https://netix.dl.sourceforge.net/project/freetts/FreeTTS/FreeTTS%201.2.2/freetts-1.2.2-bin.zip https://netix.dl.sourceforge.net/project/freetts/FreeTTS/FreeTTS%201.2.2/freetts-1.2.2-bin.zip
  2. Add to your project enter image description here
  3. 编写简单的代码。

像这样

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

public class TestVoice {


    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);

    }

}

只要确保所有这些库也在您的桌面上即可。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Freetts 无法使用外部扬声器 的相关文章

随机推荐