我如何在 Java 中的所有其他任务之前 运行 语音识别器,只有当输出包含 begin 时,程序才会继续

How do I run speech recognizer before all other tasks in Java, in such a way that only if the output contains begin, the program continues

我的程序中有一个登录页面和一个注册页面。 只有当用户说开始时,我才想 运行 它。 这些页面在我的 class 的主要方法中调用,我有一个语音识别器 class。 我希望程序仅在 String output.contains("begin") == true

时继续

我尝试将 Class.main(args) 放入我的 if(output.contains("begin") == true)) 案例中,出现未处理的异常,当我用 try and catch 包围该部分时,它不起作用。

有人告诉我从我的 API 中继承和实施 classes 会起作用,但我不太确定该怎么做。

final Microphone mic = new Microphone(FLACFileWriter.FLAC);
GSpeechDuplex duplex = new GSpeechDuplex("AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw");            
duplex.setLanguage("en");
duplex.addResponseListener(new GSpeechResponseListener() {
    String old_text = "";

    public void onResponse(GoogleResponse gr) {
        String output = gr.getResponse();
        if (gr.getResponse() == null) {
            this.old_text = response.getText();
            if (this.old_text.contains("(")) {
                this.old_text = this.old_text.substring(0, 
                this.old_text.indexOf('('));
            }                
            System.out.println("Paragraph Line Added");
            this.old_text = ( response.getText() + "\n" );
            this.old_text = this.old_text.replace(")", "").replace("( ", "");                
            response.setText(this.old_text);

        }
        if (output.contains("(")) {
            output = output.substring(0, output.indexOf('('));
        }
        if (!gr.getOtherPossibleResponses().isEmpty()) {
            output = output + " (" + (String) 
            gr.getOtherPossibleResponses().get(0) + ")";
        }
        response.setText("");
        response.append(this.old_text);
        response.append(output);

        System.out.println(output);

        if(output.contains("begin") == true){
            duplex.stopSpeechRecognition();
            mic.close();
            Trying_Different_Languages t = new Trying_Different_Languages();
            frame.dispose();
        }
    }
});

希望程序在我说开始时开始但是 当我说开始时它并没有开始。 try 和 catch 语句有助于无错误编译。

在一个程序中应该只存在 1 个 public static void main(String[] args) 方法。那是告诉你程序启动的指示器。

您应该添加一个不同的方法来在特定点执行您想要的操作,而不是调用主要方法。

所以详细来说它可以是这样的:

public class SomeClass {
    public static void someMethodName() {
        //some stuff you want to execute
    }
}

因此以及您要执行代码的位置:

...
SomeClass.someMethodName(); //executes the stuff you want.

在这种情况下,如果您创建不同的方法来准确地完成您在特定时间点需要做的事情,它就会起作用。