speech recognition in java

情到浓时终转凉″ 提交于 2019-12-03 20:25:50
if (rec != null) {
    System.out.println(rec);
}
else {
    System.out.println("rec is null");   
    // <-- here's your problem.  you need to return, exit, or throw here!
}

// Start up the recognizer
rec.allocate();  // <-- This is the line that's blowing out (I assume)

You'll get a null pointer because even though you're else is handling the case when rec is null, you're program continues. You need to return or exit, or something when rec is null.

Note: Also, I reformatted your code because it's hard to read your if/else. If you're going to use curlies on one branch of your if/else, you should use curlies on both. It makes it more readible.

Edit: Oh yeah, as far as why createRecognizer is returning null, I'm afraid I don't know.

ameen

rewrite the

// Create a recognizer that supports English.
      rec = Central.createRecognizer(
              new EngineModeDesc(Locale.ENGLISH));

to be as below

            SynthesizerModeDesc desc = new SynthesizerModeDesc(
            null,          // engine name
            "general",     // mode name
            Locale.US,     // locale
            null,          // running
            null);         // voice

     synth = Central.createSynthesizer(desc);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!