onPartialResult 始终是一个值,即使在我更改关键字之后也是如此?
onPartialResult is always one value, even after I change the keyword?
hypothesis.getHypstr()
始终是一个值,即使我更改了关键字!
我正在使用 pocketsphinx
进行语音识别,我让用户更改要听的内容。此值存储在我的共享首选项中。我的问题是 hypothesis.getHypstr()
仅在说出前一个关键字时才被调用。
例如:
如果设置为默认关键字(橙色和彩虹),则识别正常。但是,如果用户将其更改为 "hello computer",那么 onPartialResult
方法仍然只会在用户打招呼时被调用, 并且 hypothesis.getHypstr()
仍然是橙色和彩虹。
onCreate:
try {
Assets assets = new Assets(MyService.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
Log.v(TAG, "SET UP DIRECTORIES STARTING LISTENING!");
mSpeechRecognizer.startListening("usersKeyword");
} catch (IOException e) {
e.printStackTrace();
Log.v(TAG, e.toString());
}
setupRecognizer()
public void setupRecognizer(File sphinxDir) {
try {
mSpeechRecognizer = defaultSetup()
.setAcousticModel(new File(sphinxDir, "en-us-ptm"))
.setDictionary(new File(sphinxDir, "cmudict-en-us.dict"))
.setBoolean("-allphone_ci", true)
.setKeywordThreshold(1e-40f)
.getRecognizer();
} catch (IOException e) {
e.printStackTrace();
}
mSpeechRecognizer.addListener(this);
mSpeechRecognizer.addKeyphraseSearch("usersKeyword", keyword.getString("keyword", "oranges and rainbows"));
}
onPartialResult:
@Override
public void onPartialResult(Hypothesis hypothesis) {
if (hypothesis == null) { //no one spoke
return;
}
String text = hypothesis.getHypstr();
Log.v(TAG, "TEXT: " + text + "hypothesis.getHypstr: " + hypothesis.getHypstr());
if (text.equals(keyword.getString("keyword", "oranges and rainbows"))) { //Only happens when text is oranges and rainbows, even after changing preference value!!!
Log.v(TAG, "Heard user keyword!");
mSpeechRecognizer.cancel();
mSpeechRecognizer.startListening("usersKeyword");
}
}
为什么 hypothesis.getHypstr()
总是只有一个值,即使我更改了 addKeyphraseSearch
的值?
谢谢,
鲁奇尔
编辑:
实际上,每次用户更改输入时,我都会停止并启动服务,因此每次用户更改数据时都会调用 onCreate()
。
完整代码:
每次要更改关键字时都需要调用mSpeechRecognizer.addKeyphraseSearch()
。
您不需要销毁该服务,只需使用 onCreate
创建一次即可。
您可以在onStartCommand
中设置命令:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
recognizer.cancel();
recognizer.addKeyphraseSearch("usersKeywords", intent.getStringExtra("keyword"););
recognizer.startListening("usersKeywords");
}
来自另一个 class 的服务用户,您有意启动服务:
Intent i = new Intent(this, MyService);
i.putExtra("keyword", "hello");
startService(i);
有关详细信息,请阅读 documentation