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.(ConfigurationManager.java:58) at 
HelloWorld.main(HelloWorld.java:22)

我已将 HelloWord.jar 文件添加到我的项目中,但我仍然得到相同的结果!请建议...

4

1 回答 1

1

getResource("helloworld.config.xml") 指向空值。检查 url 指向的文件是否配置正确。确保它在您的类路径中。

于 2012-11-10T19:21:55.960 回答