2016-04-29 46 views
1

我試圖將我的語音識別器放入計算器應用程序中,以便盲人可以使用它。下面是代碼:將類實現到現有類中

public class HelloWorld { 

    public static Recognizer recognizer; 

    public static void init(String[] args) { 
     try { 
      URL url; 
      if (args.length > 0) { 
       url = new File(args[0]).toURI().toURL(); 
      } else { 
       url = HelloWorld.class.getResource("helloworld.config.xml"); 
      } 

      System.out.println("Loading..."); 

      ConfigurationManager cm = new ConfigurationManager(url); 

      recognizer = (Recognizer) cm.lookup("recognizer"); 
      Microphone microphone = (Microphone) cm.lookup("microphone"); 


      /* allocate the resource necessary for the recognizer */ 
      recognizer.allocate(); 

      /* the microphone will keep recording until the program exits */ 
      if (microphone.startRecording()) { 

       System.out.println 
        ("Say: (Good morning | Hello) " + 
        "(Bhiksha | Evandro | Paul | Philip | Rita | Will)"); 


       System.out.println 
        ("Start speaking. Press Ctrl-C to quit.\n"); 

       /* 
       * This method will return when the end of speech 
       * is reached. Note that the endpointer will determine 
       * the end of speech. 
       */ 

      } 
     } catch (IOException e) { 
      System.err.println("Problem when loading HelloWorld: " + e); 
      e.printStackTrace(); 
     } catch (PropertyException e) { 
      System.err.println("Problem configuring HelloWorld: " + e); 
      e.printStackTrace(); 
     } catch (InstantiationException e) { 
      System.err.println("Problem creating HelloWorld: " + e); 
      e.printStackTrace(); 
     } 
    } 

    public static int convertToInt(String num){ 
     int tmp = 0; 
     switch(num){ 

      case "one": tmp = 1; break; 
      case "two": tmp = 2; break; 
      case "three": tmp = 3; break; 
      case "four": tmp = 4; break; 
      case "five": tmp = 5; break; 
      case "six": tmp = 6; break; 
      case "seven": tmp = 7; break; 
      case "eight": tmp = 8; break; 
      case "nine": tmp = 9; break; 
      default: tmp = -1; 
     } 
     System.out.print("The number is: " + tmp); 
     return tmp; 
    } 

    public static int check(){ 
     Result result = recognizer.recognize(); 
     String resultText = ""; 
     if (result != null) { 
      resultText = result.getBestFinalResultNoFiller(); 

      System.out.println("-"+resultText+"-"); 
      System.out.println("You said: " + resultText + "\n"); 
     } else { 
      System.out.println("I can't hear what you said.\n"); 
     } 
     return convertToInt(resultText); 
    } 
} 

,這裏是該應用的主循環:

public static void main(String[] args) { 
    HelloWorld.init(args); 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      new Main(); 
      HelloWorld.check(); 
     } 
    }); 
} 

我似乎無法讓他們在同一時間運行,任何人都可以幫我想出解決辦法?

+0

您的主類定義在哪裏? –

+0

'main'屬於哪個類? 'HelloWorld.main'在哪裏,它有什麼作用?什麼是「主」? – RealSkeptic

+0

@RealSkeptic全部修復 – Luke

回答

1

您需要兩個不同的線程。運行在Swing線程中的Main和運行在另一個線程中的HelloWorld主線程。

目前還不清楚他們如何溝通。無論如何,這是你如何能在兩個不同的線程啓動:

public static void main(String[] args) { 
    HelloWorld.init(args); 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      new Main(); 
     } 
    }); 
    HelloWorld.check(); 
} 

但是請注意,這個代碼將不會解決你的問題。你可以閱讀關於Swing線程here