【发布时间】:2019-01-13 23:37:04
【问题描述】:
我正在制作一个返回音频但仅持续 x 秒的函数。
如果用户说
给我播放一首歌 x 秒
我正在播放歌曲,但需要在 X 秒后取消。因此,我的计划是在运行音频之前启动计时器 x 秒。然后,一旦计时器到了,就将通知设置到另一个函数,该函数将返回一个新的声音,说你的时间到了。
虽然我需要一种方法来启动后台计时器,但我已经搞定了逻辑。
def start_audio():
card_title = "Start Audio"
session_attributes = {}
sound_url = 'https://s3.amazonaws.com/........mp3'
speech_output = "<speak><audio src='{}'/> </speak>".format(sound_url)
should_end_session = False
return build_response(session_attributes, myCustomSSML(
card_title, speech_output, None, should_end_session))
def myLaunchIntent():
""" If we wanted to initialize the session to have some attributes we could
add those here
"""
session_attributes = {}
card_title = "Welcome"
speech_output = "Welcome to your custom Alexa application!"
return start_audio()
目前:
-> 启动时
-> 返回开始音频
-> 我需要通过 X 秒
-> X 秒将启动计时器
-> 一旦计时器完成调用另一个停止的函数
如果有人可以引导我朝着正确的方向前进,例如向我展示如何在 x 秒内运行后台计时器然后调用另一个函数,那么我将能够接管并将其扩展得更高。
【问题讨论】:
-
必须是定时器吗?您可以将歌曲开始的时间存储在变量中,例如 startTime。然后反复检查 currentTime - startTime === 用户所需的长度。也许在一个while循环中,虽然我不确定这是多么优化。
标签: python python-3.x alexa alexa-skills-kit