0 Replies Latest reply: Nov 5, 2011 1:53 PM by 898165 RSS

    Suitable Engine

    898165
      I used HelloWorld sample code from http://java.sun.com/products/java-media/speech/forDevelopers/jsapi-guide/Recognition.html#7408
      import java.util.logging.Level;
      import java.util.logging.Logger;
      import javax.speech.*;
      import javax.speech.recognition.*;
      import java.io.FileReader;
      import java.util.Locale;
      
      public class Main 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.getSpokenText() + " ");
                System.out.println();
      try {
      // Deallocate the recognizer and exit
      rec.deallocate();
      } catch (EngineException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
      } catch (EngineStateError ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
      }
                System.exit(0);
           }

           public static void main(String args[]) {
                try {

                     // Create a recognizer that supports English.

      EngineModeDesc mode =new EngineModeDesc(Locale.ENGLISH);
      rec = Central.createRecognizer(mode);
      //rec = Central.createRecognizer(new EngineModeDesc(null, null,java.util.Locale.ENGLISH, null));
      //rec = Central.createRecognizer();
      /*EngineList list = Central.availableRecognizers(mode);
      if(list.isEmpty())
      System.out.println("Recognizer yok");

      if(mode==null)
      System.out.println("MODE IS NULL");
      else
      System.out.println("MODE IS NOT NULL");

      if (rec == null)
      System.out.println("REC IS NULL");
      else
      System.out.println(rec);
      //EngineList list = Central.availableRecognizers(rec);*/

                     // Start up the recognizer
                     rec.allocate();
      System.out.println("HATA");
                     // Load the grammar from a file, and enable it
                     //FileReader reader = new FileReader(args[0]);
      String gramer="C:\\Users\\zackeriya\\Documents\\NetBeansProjects\\Hello_World_1\\dil.gram";
      FileReader reader = new FileReader(gramer);
                     RuleGrammar gram = rec.loadJSGF(reader);
                     gram.setEnabled(true);

                     // Add the listener to get results
                     rec.addResultListener(new Main());

                     // Commit the grammar
                     rec.commitChanges();

                     // Request focus and start listening
                     rec.requestFocus();
                     rec.resume();
                } catch (Exception e) {
                     e.printStackTrace();
                }
           }
      }
      When I compiled, I got this error "java.lang.NullPointerException at Main.main(Main.java:56)" ---> 
      56. rec.allocate();
       ... After long search I learnt that If no suitable engine is available, the methods return null. Here is the code which returns null 
      rec = Central.createRecognizer(mode);
      ... So how can I get or install engine to my computer ?