如何结束 Listen 动作

How to end Listen action

我正在努力了解如何以及是否可能在 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 can not speak while is listening从而阻止任务的正确结束。 我发现唯一可能允许终止操作的方法是 requestCancellation() 但在我的情况下它似乎不起作用,布尔方法 isCancelled ()检查动作是否终止一直返回False.

实际上可以停止 Listen 操作,还是我必须完全改变代码的结构方式(即从一开始就使用聊天机器人)?

当你调用listen.requestCancellation()时,你实际上是在取消building的监听动作。 您定义 listen 为: listen = ListenBuilder.with(qiContext).withPhraseSet(response).buildAsync();.

相反,您应该取消在 运行 操作时返回的未来:result = heardPhrase.run();。 调用 result.requestCancellation() 应该可以解决问题。

经验法则是 Qi SDK 操作首先 built 然后 运行.