【发布时间】:2014-02-03 02:55:36
【问题描述】:
我在使用 Java 的 Sphinx 语音识别库时遇到问题。我正在使用它来获取输入并处理它。我第一次得到输入时,它起作用了。第二次,它在我有机会说话之前立即回答。在那之后,它只是继续回答自己。我尝试在每次输入之前分配并在每次输入之后取消分配,但这似乎不起作用。我能做什么?
代码:
这是处理获取输入的方法:
public void getInput() {
if (using) return;
using = true;
if (!allocated) {
JTalk.speak("Please hold.");
recognizer.allocate();
allocated = true;
}
JTalk.speak("Speak now.");
Result result = recognizer.recognize();
if (result != null) {
String resultText = result.getBestFinalResultNoFiller();
JDispatcher.getInstance().matchInput(resultText);
}
else {
JTalk.speak("Try again.");
}
using = false;
}
你需要知道的:
- 这是从
MouseListener调用的TrayIcon。 -
speak(String)从Runtime运行say <text>。 -
matchInput(String)遍历数组中所有已注册的侦听器并测试匹配项。
更新 2:
根据 Nikolay Shmyrev 的回答,我尝试在构造函数中分配麦克风并在 getInput() 中的适当时间启动然后停止麦克风。
这是 SphinxBridge 类:
public class SphinxBridge {
private ConfigurationManager cm;
private Recognizer recognizer;
private Microphone microphone;
private boolean using = false;
public SphinxBridge() {
this.cm = new ConfigurationManager(SphinxBridge.class.getResource("input.config.xml"));
this.recognizer = (Recognizer) cm.lookup("recognizer");
this.microphone = (Microphone) cm.lookup("microphone");
recognizer.allocate();
}
public void getInput() {
if (using) return;
using = true;
if (!microphone.startRecording()) {
JTalk.speak("Cannot start microphone.");
return;
}
JTalk.speak("Speak now.");
Result result = recognizer.recognize();
if (result != null) {
String resultText = result.getBestFinalResultNoFiller();
JDispatcher.getInstance().matchInput(resultText);
}
else {
JTalk.speak("Try again.");
}
microphone.stopRecording();
using = false;
}
}
但是,这仍然不起作用。首先,它工作正常。但是,对于所有后续时间,它会同时显示 Speak now 和 Try again。
解决方案:
从上面的代码中,我简单地添加了
microphone.clear();
在开始录制的那一行之上。
【问题讨论】:
-
分享代码可能会有所帮助。
-
已添加。我最初没有共享代码,因为我认为这可能是已知的 Sphinx 错误,或者有人有一个快速的解决方案。我现在意识到我应该首先添加它。对不起。
-
@nrubin29 你能分享你的项目吗
-
@Abderrahim 我不认为我有这个代码了。如果您遇到类似的问题,我可以尽力帮助您。