[英]How to end Listen action
I'm struggling to understand how, and if it is possible, to end a listen action on Pepper after it is started.我正在努力理解如何以及如果可能的话,在 Pepper 启动后结束监听操作。 What i want to achieve in my application is the following:
我想在我的应用程序中实现以下目标:
This is the part of the code to handle voice input, and it works as expected:这是处理语音输入的代码部分,它按预期工作:
public void onRobotFocusGained(QiContext qiContext) {
...
//Pepper greets the approached user
animation_future = greeting_animation.async().run();
//After the animation is finished, wait for human input
animation_future.andThenConsume(chatting ->{
listen = ListenBuilder.with(qiContext).withPhraseSet(response).buildAsync();
listen.andThenConsume(heardPhrase->{
//Pepper start listening
result = heardPhrase.run();
//If the response contains "yes"
if( (result.getHeardPhrase().getText().toLowerCase()).equals("si")){
//User need helps, Pepper start discussing with it
helpNeeded_chat.async().run();
//Otherwise
}else if( (result.getHeardPhrase().getText().toLowerCase()).equals("no")){
//No help is required, Pepper says goodbye
animation_future = goodbye_animation.async().run();
//A new user comes by - Restart scenario
animation_future.andThenConsume(restart->{
Log.i(TAG, "Interaction ended. Restarting.");
//Restart by starting this same activity
startActivity(new Intent(this, MainActivity.class));
});
}
});
});
}
While this is the part that handles touch inputs (defined in its own method outside onRobotFocusGained):虽然这是处理触摸输入的部分(在 onRobotFocusGained 之外的自己的方法中定义):
final Button button_no = findViewById(R.id.button_no);
button_no.setOnClickListener(v->{
...
listen.requestCancellation();
//Help is refused - Restart scenario
animation_future = goodbye_animation.async().run();
animation_future.andThenConsume(restart->{
Log.i(TAG, "Interaction ended. Restarting.");
//Restart by starting this same activity
startActivity(new Intent(this, MainActivity.class));
});
});
In this case since the Listen action keeps running then the warning Pepper can not speak while is listening is thrown thus preventing correct ending of the task.在这种情况下,由于 Listen 操作一直在运行,因此会引发警告Pepper 在侦听时无法说话,从而阻止正确结束任务。 What I have found is that the only method that might allow to terminate actions is requestCancellation() but it doesn't seems to work in my case, the boolean method isCancelled() to check whether the action is terminated keeps returning always False .
我发现唯一可能允许终止操作的方法是requestCancellation()但在我的情况下它似乎不起作用,用于检查操作是否终止的 boolean 方法isCancelled()始终返回False 。
It is actually possible to stop the Listen action or do I have to entirely change the way in which my code is structured (ie use a chatbot from the very beginning)?实际上可以停止 Listen 操作,还是我必须完全改变代码的结构方式(即从一开始就使用聊天机器人)?
When you are calling listen.requestCancellation()
, you are in fact cancelling the building of the listen action.当您调用
listen.requestCancellation()
时,实际上是在取消侦听操作的构建。 You defined listen
with: listen = ListenBuilder.with(qiContext).withPhraseSet(response).buildAsync();
您定义了
listen
: listen = ListenBuilder.with(qiContext).withPhraseSet(response).buildAsync();
. .
Instead, you should cancel the future returned when you run the action: result = heardPhrase.run();
相反,您应该取消运行操作时返回的未来:
result = heardPhrase.run();
. . Calling
result.requestCancellation()
should do the trick for you.调用
result.requestCancellation()
应该可以为您解决问题。
The thumb rule is that Qi SDK actions are first built then run .经验法则是 Qi SDK 动作首先构建然后运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.