Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java onPartialResult始终为一个值,即使在我更改关键字之后?_Java_Android_String_Voice Recognition_Cmusphinx - Fatal编程技术网

Java onPartialResult始终为一个值,即使在我更改关键字之后?

Java onPartialResult始终为一个值,即使在我更改关键字之后?,java,android,string,voice-recognition,cmusphinx,Java,Android,String,Voice Recognition,Cmusphinx,假设。getHypstr()始终是一个值,即使我更改了关键字 我正在使用pocketsphinx进行语音识别,我允许用户更改要听的内容。此值存储在我的共享首选项中。我的问题是,只有在说出前一个关键字时才会调用假设.getHypstr() 例如: try { Assets assets = new Assets(MyService.this); File assetDir = assets.syncAssets(); setu

假设。getHypstr()
始终是一个值,即使我更改了关键字

我正在使用pocketsphinx进行语音识别,我允许用户更改要听的内容。此值存储在我的共享首选项中。我的问题是,只有在说出前一个关键字时才会调用
假设.getHypstr()

例如:

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());
        }
@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");

    }

}
如果将其设置为默认关键字(橙色和彩虹),则识别效果良好。但是,如果用户将其更改为“hello computer”,则仅当用户说hello、
假设时才会调用
onPartialResult
方法。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());
        }
@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");

    }

}
设置识别器()

部分结果:

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());
        }
@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");

    }

}
为什么
假设.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");
}
从作为服务用户的另一个类中,您启动服务的目的是:

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

更多详情请阅读

,但我有
mSpeechRecognizer.addKeyphraseSearch(“usersKeyword”,keyword.getString(“keyword”,“橙子和彩虹”)在我的
设置识别器
方法中。那不管用吗?非常感谢!:)是的,但是您只调用一次
setupRecognizer()
。实际上,每次用户更改输入时,我都会停止并启动服务,因此每次用户更改数据时都会调用
onCreate()
。我已经编辑了这个问题。请让我知道我现在该做什么。谢谢:)很难说,可能是逻辑流错误。如果你发布完整的源代码进行复制,我可以提供帮助。也许我需要在制作之前删除所有资产文件?是否有类似于
assets.deleteSync
的方法?您需要显示完整的代码,而不仅仅是部分代码。@NikolayShmyrev问题在于
假设.getHypstr()
总是
橙子和彩虹
,即使我更改了用户关键字。@NikolayShmyrev也许我需要在创建之前删除所有资产文件?是否有类似于
assets.deleteSync
的方法?