[英]Exception in java speech recognition
我在以下代码中遇到一些异常:
import java.io.FileReader;
import java.io.IOException;
import java.util.Locale;
import javax.speech.AudioException;
import javax.speech.Central;
import javax.speech.EngineException;
import javax.speech.EngineModeDesc;
import javax.speech.EngineStateError;
import javax.speech.recognition.GrammarException;
import javax.speech.recognition.Recognizer;
import javax.speech.recognition.Result;
import javax.speech.recognition.ResultAdapter;
import javax.speech.recognition.ResultEvent;
import javax.speech.recognition.ResultToken;
import javax.speech.recognition.RuleGrammar;
public class HelloWorld extends ResultAdapter {
static Recognizer rec;
// Receives RESULT_ACCEPTED event: print it, clean up, exit
@Override
public void resultAccepted(ResultEvent e) {
Result r = (Result) (e.getSource());
ResultToken tokens[] = r.getBestTokens();
for (int i = 0; i < tokens.length; i++) {
System.out.print(tokens[i].getSpokenText() + " ");
}
System.out.println();
try {
// Deallocate the recognizer and exit
rec.deallocate();
} catch (EngineException | EngineStateError ex) {
System.out.println(ex.toString());
}
System.exit(0);
}
public static void main(String args[]) {
try {
// Create a recognizer that supports English.
rec = Central.createRecognizer(
new EngineModeDesc(Locale.ENGLISH));
// Start up the recognizer
rec.allocate();
// Load the grammar from a file, and enable it
FileReader reader = new FileReader(args[0]);
RuleGrammar gram = rec.loadJSGF(reader);
gram.setEnabled(true);
// Add the listener to get results
rec.addResultListener(new HelloWorld());
// Commit the grammar
rec.commitChanges();
// Request focus and start listening
rec.requestFocus();
rec.resume();
} catch (IllegalArgumentException | EngineException | SecurityException | GrammarException | IOException | AudioException e) {
e.printStackTrace();
}
}
}
异常:-Demo5.HelloWorld.main(HelloWorld.java:55)上的线程“ main”中的异常java.lang.NullPointerException Java结果:1
此行(第55行)中发生了异常:
// Start up the recognizer
rec.allocate();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.