2012-03-02 24 views
1

嗨,我正在製作一個軟件,我想在其中開發語音軟件的Java ...我在java中運行「Hello」sphinx代碼。Java 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) 

請建議...

感謝

+0

也許你helloworld.config.xml找不到 – Sibbo 2012-03-02 11:24:32

回答

1

目前尚不清楚這行是實際上第22行代碼,但我懷疑是這樣的:

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

我的猜測是無法找到,所以getResource返回null,你傳遞給ConfigurationManager

有幾個原因的配置可能無法找到各種理由,如:

  • 這不是在類路徑中(例如,不捆綁成一個jar文件,而不是複製到bin目錄中的Eclipse)
  • 這不是在正確的地方 - 該代碼會試圖找到它相對於HelloWorld.class,而你可能有它的「包根」

很難多說,而無需您更多的信息。

假設的情況下,它與斯芬克斯無關。

2

我有同樣的錯誤,並修復它。您只需將發現斯芬克斯bin文件夾一個文件HelloWord.jar只包括JAR文件到您的項目,它就會恢復正常運行。


JAR文件的路徑: sphinx4-1.0beta6斌\ sphinx4- 1.0beta6 \ bin \ HelloWorld.jar

+1

這是一個正確的答案 – jmishra 2012-08-21 03:46:20

1

如果您使用的是src版本,請確保您有lib/sphinx4.jar,如果沒有轉到頂層目錄(例如sphinx4.0beta-src)並鍵入ant。

然後你可以運行它鍵入:

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

如果您想了解更多信息,請訪問:CMU-Sphinx-Helloword