运行 背景中的 sphinx4 识别器 UI

Run sphinx4 recognizer in background with UI

我正在尝试将现有的基于控制台的 Java 程序(使用 sphinx 的语音识别项目)制作成基于 GUI 的程序。我尝试更改代码,但现有程序不会 运行 如果我更改它。

我现有的识别代码 (full code),它在没有 GUI 的情况下工作得很好:

public class HelloWorld {
    /**
     * Main method for running the HelloWorld demo.
     */
    static int i=1;
    static String resultText;public static void main(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 = (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: (Command | Program| Browser | Bluetooth |  Device Manager |Power Options |Cal | Control | Player |task manager | Windows Security Center)");
                System.out.println("Say: ( open word | open phot oshop|open Access|start Excel|start nero |start fire wall| open Pad |open Paint)");
            while (true) 
            {
            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.
                     */ 
            Result result = recognizer.recognize();
            if (result != null)
            {
                    System.out.println("Enter your choise"+ "\n");
             resultText = result.getBestFinalResultNoFiller();
            System.out.println("You said: " + resultText + "\n");
//          Applications*********************************************
            if(resultText.equalsIgnoreCase("Command Prompt"))
            {
                try{
                    Runtime.getRuntime().exec("cmd /c start cmd");
                    }
                catch(Exception er){    
                }
            }
//          Simulate action commands by importing the robot class above
            if(resultText.equalsIgnoreCase("scroll up"))
            {
                try {
                    Robot r = new Robot();
                    r.keyPress(KeyEvent.VK_UP);
                    r.delay(500);
                    r.keyPress(KeyEvent.VK_UP);
                    r.delay(500);
                    r.keyPress(KeyEvent.VK_UP);
                } catch (AWTException e) {
                    e.printStackTrace();
                }
            }
//          Program Action Command ABOUT
            else if(resultText.equalsIgnoreCase("recognition stop"))
            {
                try{

                    //recognizer.wait();
                    System.out.println("See you later!");
                    System.exit(0);}
                catch(Exception estop ){}
            }                       
            else
            {
                        System.out.println("I can't hear what you said.\n");
            }
       }
       } 
        else
        {
            System.out.println("Cannot start microphone.");
            recognizer.deallocate();
            System.exit(1);
        }

        } 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();
        }

    }
}

这是我希望现有程序启动的 Gui 代码 (full code):

    JButton btnNewButton = new JButton("Start Recognizing");
    btnNewButton.setBackground(UIManager.getColor("Button.background"));
    btnNewButton.setForeground(new Color(34, 139, 34));
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            state.setText("Listening");
            System.out.println("Started Listening");
            state.setBackground(new Color(51, 204, 0));

            // Start recognizing from the existing program

        }
    });

以及我要pause/stop录音的部分:

    JButton btnNewButton_1 = new JButton("Stop Recognizing");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            state.setText("Not listening");
            state.setBackground(new Color(204, 0, 51));
            System.out.println("Stopped Listening");

            // Pause/Stop recognition

        }
    })

我该怎么做?

您需要运行在后台线程中识别。这样您就可以与 GUI 交互并同时进行识别。

您可以查看以下教程以了解如何实现:

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

你不能有2个class包含main函数的es如果你想使用它们together.You必须使用frame1中的main函数class,创建一个hello world对象class 并使用此调用 hello world 的函数 object.The 这些函数的调用必须从 frame1 的事件处理程序中完成。

使用SwingWorker, run the recognizer in your implementation of doInBackground() using ProcessBuilder as shown here。在 process().

的实施中,您可以 publish() 中间结果和 append() 它们到 JTextArea

附录:查看 API, it should be possible to skip ProcessBuilder and instantiate a LiveSpeechRecognizer directly, as shown here。然后,您的 publish() 实现可以通过 getResult().

迭代 SpeechResult#getWords() 返回的 List<WordResult>