onPartialResult总是一个值,即使我更改关键字后?

时间:2016-03-03 03:39:21

标签: java android string voice-recognition cmusphinx

即使我更改了关键字,

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的值?

谢谢,

Ruchir

修改: 实际上,每次用户更改输入时,我都会停止并启动服务,因此每次用户更改数据时都会调用onCreate()

完整代码:

https://gist.github.com/anonymous/47efc9c1ca08d808e0be

2 个答案:

答案 0 :(得分:0)

每次要更改关键词时,都需要致电mSpeechRecognizer.addKeyphraseSearch()

答案 1 :(得分:0)

您无需销毁该服务,只需使用onCreate创建一次。

您可以在onStartCommand

中设置命令
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
     recognizer.cancel();
     recognizer.addKeyphraseSearch("usersKeywords", intent.getStringExtra("keyword"););
     recognizer.startListening("usersKeywords");
}

从作为服务用户的另一个类开始使用意图服务:

 Intent i = new Intent(this, MyService);
 i.putExtra("keyword", "hello");
 startService(i);

有关详细信息,请参阅documentation