java - 使用 UI 在后台运行 sphinx4 识别器

标签 java swing user-interface sphinx4

我正在尝试制作我现有的基于控制台的 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

        }
    });

以及我想要暂停/停止录制的部分:

    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

        }
    })

我该怎么做?

最佳答案

使用 SwingWorker ,在 doInBackground() 的实现中运行识别器使用ProcessBuilder如图here 。您可以publish()中期业绩和append()他们到JTextArea在您实现 process() 时.

附录:查看API ,应该可以跳过ProcessBuilder并实例化 LiveSpeechRecognizer直接,如图here 。您的 publish() 的实现然后可以迭代 List<WordResult>返回者 SpeechResult#getWords()通过getResult() .

关于java - 使用 UI 在后台运行 sphinx4 识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28971487/

相关文章:

java - Grails/Java new Date() 在 GSP 中不同

java - 删除重复的项目java gui

JQuery UI Autocomplete 自动触发 (IE)

java - 计算字符串中字符的出现次数(频率)

java - 在 Java Swing 中构建简单日历的方法

java - SQLITE 过滤查询搜索

java - 如何使用 Google Guice 注入(inject)依赖项

java - Java中二维数组转一维数组

java - 如何让一个集合保留包含我在java中指定子字符串的所有字符串?

java - 如何使 JTextarea 中的特定文本不可编辑