【发布时间】:2020-08-09 08:57:16
【问题描述】:
我正在努力理解如何(如果可能的话)在 Pepper 启动后结束监听操作。我想在我的应用程序中实现以下目标:
- 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));
});
});
在这种情况下,由于 Listen 操作一直在运行,因此会引发警告 Pepper 在聆听时无法说话,从而阻止正确结束任务。 我发现唯一可能允许终止操作的方法是 requestCancellation() 但在我的情况下它似乎不起作用,布尔方法 isCancelled() 检查动作是否终止总是返回False。
实际上可以停止 Listen 操作,还是我必须完全改变代码的结构方式(即从一开始就使用聊天机器人)?
【问题讨论】: