首页 > 解决方案 > Alexa Skills Kit(AudioPlayer 接口):在 SessionEndedRequest 之后没有提示的音频恢复

问题描述

我一直在为我的 Alexa 技能添加新闻通讯功能,但在结束音频流时我遇到了一些奇怪的行为。

tl; dr 为什么我退出技能后音频会恢复,即使我在退出前暂停/停止它

这是我的意思的一个例子:

"play the newsletter."
-> the newsletter plays
"Alexa, pause"
-> PauseIntent is called, which gives a StopDirective - ending the audio.
"Exit."
-> SessionEndedRequest is received, but then the audio *starts to play again*

我能够让它工作的唯一方法是调用 PauseIntent,然后说 Alexa,Stop,它调用 Stop/Cancel 意图,它给出一个停止指令并将should_end_session设置为 true。

我以为我可以在 SessionEndedRequest 处理程序返回的响应中添加另一个 StopDirective,但事实证明Your skill cannot return a response to SessionEndedRequest.

我的代码与示例代码没有任何不同,所以我很好奇这是否是预期的行为。

以下是相关代码:

class PauseIntentHandler(AbstractRequestHandler):
    """Single handler for Cancel and Stop Intent."""

    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return ask_utils.is_intent_name("AMAZON.PauseIntent")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        logger.info('|============== Entered PauseIntent =============|')
        offset = get_offset(handler_input)

        set_session_attr(handler_input, 'offset', offset)

        speak_output = 'Okay. You can say stop to exit, or resume to continue.'
        reprompt = 'You can say stop to exit, or resume to continue.'


        return (
            handler_input.response_builder
            .speak(speak_output)
            .ask(reprompt)
            .add_directive(StopDirective())
            .set_should_end_session(False)
            .response
class CancelOrStopIntentHandler(AbstractRequestHandler):
    """Single handler for Cancel and Stop Intent."""

    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return (ask_utils.is_intent_name("AMAZON.CancelIntent")(handler_input) or
                ask_utils.is_intent_name("AMAZON.StopIntent")(handler_input))
                

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        logger.info('|============== Entered CancelIntent =======|')


        speak_output = "Okay. Goodbye!"

        return (
            handler_input.response_builder
            .speak(speak_output)
            .add_directive(StopDirective())
            .set_should_end_session(True)
            .response
        )

提前致谢。

标签: pythonalexaalexa-skills-kit

解决方案


推荐阅读