虽然我尝试了来自here的简单语音识别器JSAPI(JSR 113)(此链接现在不起作用)或here遵循该库的示例,但我得到了java.lang.NullPointerException。该错误是由“rec.allocate();”行引起的。为什么会在识别器上面分配一行(“rec =(Recognizer)EngineManager.createEngine(mode);”)?
代码如下:
import javax.speech.*;
import javax.speech.recognition.*;
...
static Recognizer rec;
...
public static void run(String[] args) {
try {
RecognizerMode mode = new RecognizerMode(new SpeechLocale("en", "GB"));
rec = (Recognizer) EngineManager.createEngine(mode);
// Start up the recognizer
rec.allocate();
// Load the grammar, and enable it
String grammarMarkup =
"<grammar root='sentence' xml:lang='en' version='1.0' " +
"xmlns='http://www.w3.org/2001/06/grammar'>" +
"<rule id='sentence' scope='public'>" +
"<one-of>" +
"<item> hello world </item>" +
"<item> good morning </item>" +
"<item> hello mighty computer </item>" +
"</one-of>" +
"</rule>" +
"</grammar>";
rec.getGrammarManager().loadGrammar("grammar:STT", null, grammarMarkup);
// Add the listener to get results
rec.addResultListener((ResultListener) new STT());
// Request focus and start listening
rec.requestFocus();
rec.resume();
rec.waitEngineState(Engine.DEALLOCATED);
} catch (Exception e) {
e.printStackTrace();
}
}