在背景UI运行sphinx4识别(Run sphinx4 recognizer in backgro

2019-10-22 05:15发布

我试图让我现有的基于控制台的Java程序是使用语音识别项目狮身人面像 ,到基于GUI的程序。 我试图改变代码,但如果我改变它现有的程序不运行。

我现有的用于识别(代码完整的代码 ),它完美的作品没有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();
        }

    }
}

这是贵,我想现有的程序来启动(代码完整的代码 ):

    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

        }
    });

而在这里我要暂停的部分/停止录音:

    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

        }
    })

我该怎么做?

Answer 1:

使用SwingWorker ,在你执行运行识别doInBackground()使用ProcessBuilder如图所示这里 。 你可以publish()中期业绩和append()他们到一个JTextArea在执行process()

附录:综观API ,它应该是可以跳过ProcessBuilder和实例化一个LiveSpeechRecognizer直接,如图所示这里 。 你的实现的publish()然后可以遍历List<WordResult>由归国SpeechResult#getWords()通过getResult()



Answer 2:

您需要在后台线程中运行的认可。 这样,你就能够与GUI交互,并在同一时间认识。

您可以查看下面的教程,以了解其可以实现的认识:

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



Answer 3:

你不能有一个包含主要功能2类,如果你想使用它们together.You必须使用帧1类主要功能,创建的Hello World类的一个对象,并使用这些功能这object.The电话呼叫的Hello World的功能必须从帧1的事件处理程序来完成。



文章来源: Run sphinx4 recognizer in background with UI