1

嗨,我正在用 java 制作一个软件,我想在其中开发一个语音软件......我正在用 java 运行一个“Hello”sphinx 代码。

import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;

public class HelloWorld {

    public static void main(String[] args) {
        ConfigurationManager cm;

        if (args.length > 0) {
            cm = new ConfigurationManager(args[0]);
        } else {
            cm = new ConfigurationManager(HelloWorld.class.getResource("helloworld.config.xml"));
        }

        Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
        recognizer.allocate();

        // start the microphone or exit if the programm if this is not possible
        Microphone microphone = (Microphone) cm.lookup("microphone");
        if (!microphone.startRecording()) {
            System.out.println("Cannot start microphone.");
            recognizer.deallocate();
            System.exit(1);
        }

        System.out.println("Say: (Good morning | Hello) ( Bhiksha | Evandro | Paul | Philip | Rita | Will )");

        // loop the recognition until the programm exits.
        while (true) {
            System.out.println("Start speaking. Press Ctrl-C to quit.\n");

            Result result = recognizer.recognize();

            if (result != null) {
                String resultText = result.getBestFinalResultNoFiller();
                System.out.println("You said: " + resultText + '\n');
            } else {
                System.out.println("I can't hear what you said.\n");
            }
        }
    }
}

当我尝试运行这个程序时,我得到了这个错误......

Exception in thread "main" java.lang.NullPointerException
    at edu.cmu.sphinx.util.props.SaxLoader.load(SaxLoader.java:74)
    at edu.cmu.sphinx.util.props.ConfigurationManager.<init>(ConfigurationManager.java:58)
    at HelloWorld.main(HelloWorld.java:22)

请建议...

谢谢

4

3 回答 3

3

我有同样的错误并修复它。您只需在 sphinx bin 文件夹中找到一个 HelloWord.jar 文件,只需将 jar 文件包含到您的项目中,它就会正常运行


jar 文件的路径:sphinx4-1.0beta6-bin\sphinx4-1.0beta6\bin\HelloWorld.jar

于 2012-03-28T10:27:04.287 回答
2

目前尚不清楚代码中的哪一行实际上是第 22 行,但我怀疑是这样的:

cm = new ConfigurationManager(HelloWorld.class.getResource("helloworld.config.xml"));

我的猜测是helloworld.config.xml找不到,因此getResource返回 null,您将其传递给ConfigurationManager.

找不到配置的原因有多种,例如:

  • 它不在类路径上(例如,没有捆绑到 jar 文件中,没有bin被 Eclipse 复制到目录中)
  • 它不在正确的位置 - 该代码将尝试相对于 找到它HelloWorld.class,而您可能将它放在“包根目录”中

如果没有您提供的更多信息,很难说更多。

假设这样的话,它与狮身人面像无关。

于 2012-03-02T11:26:51.410 回答
2

如果您使用的是 src 版本,请确保您有 lib/sphinx4.jar,如果没有转到顶级目录(例如 sphinx4.0beta-src)并键入 ant。

然后你可以运行它输入:

sphinx4> java -mx256m -jar bin/HelloWorld.jar

如果您想了解更多信息,请访问:CMU-Sphinx-Helloword

于 2012-11-14T14:53:06.070 回答