我正在努力了解在启动Pepper之后如何以及是否有可能结束对Pepper的监听动作。我想在我的应用程序中实现以下目标:
这是代码中处理语音输入的部分,它可以按预期工作:
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));
});
}
});
});
}
这是处理触摸输入的部分(在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));
});
});
在这种情况下,由于“监听”操作一直在运行,因此会发出警告 胡椒粉在监听时无法讲话 ,从而阻止正确结束任务。 我发现,可能允许终止操作的唯一方法是 requestCancellation(),但在我看来,布尔方法 isCancelled()似乎不起作用>检查操作是否终止总是返回 False 。
实际上可以停止侦听操作,还是我必须完全改变代码的结构方式(即从一开始就使用聊天机器人)?
答案 0 :(得分:0)
呼叫listen.requestCancellation()
时,实际上是在取消监听动作的构建。
您使用以下内容定义了listen
:
listen = ListenBuilder.with(qiContext).withPhraseSet(response).buildAsync();
。
相反,您应该取消执行操作result = heardPhrase.run();
时返回的未来。
致电result.requestCancellation()
应该可以解决问题。
经验法则是,首先构建然后运行 。