[英]How to listen to button action add combobox and listen to action again Java
[英]How to end Listen action
我正在努力理解如何以及如果可能的話,在 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()但在我的情況下它似乎不起作用,用於檢查操作是否終止的 boolean 方法isCancelled()始終返回False 。
實際上可以停止 Listen 操作,還是我必須完全改變代碼的結構方式(即從一開始就使用聊天機器人)?
當您調用listen.requestCancellation()
時,實際上是在取消偵聽操作的構建。 您定義了listen
: listen = ListenBuilder.with(qiContext).withPhraseSet(response).buildAsync();
.
相反,您應該取消運行操作時返回的未來: result = heardPhrase.run();
. 調用result.requestCancellation()
應該可以為您解決問題。
經驗法則是 Qi SDK 動作首先構建然后運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.