我的程序中有一个登录页面和一个注册页面。
我只想在用户说开始时运行它。
这些页面在我的类的主方法中调用,我有一个语音识别器类。
我希望程序仅在 String output.contains("begin") == true
我尝试将 Class.main(args)
放入我的 if(output.contains("begin") == true))
案例中,有一个未处理的情况异常(exception),当我用 try 和 catch 包围该部分时,它不起作用。
有人告诉我,从我的 API 继承和实现类是可行的,但我不太确定该怎么做。
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.
在这种情况下,如果您创建不同的方法来完全满足您在特定点需要执行的操作,那么它就会起作用。
关于java - 如何在 Java 中的所有其他任务之前运行语音识别器,这样只有当输出包含 begin 时,程序才会继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53979707/