在后台运行计时器x秒钟[Alexa Skill]

时间:2019-01-13 23:37:05

标签: python python-3.x alexa alexa-skills-kit

我正在制作一个仅返回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秒内运行后台计时器,然后调用另一个函数,那么我将能够接管并将其扩展到更高的水平。

2 个答案:

答案 0 :(得分:0)

由于Alexa用作请求/响应应用程序,因此,一旦发送响应,就没有更多的方法可以控制播放,直到用户做某件事并且有新请求发送给处理程序为止。

我可以想象两种实现所需功能的可能方法。要么即时生成一个简短的音频剪辑(使用FFmpeg之类的文件),要么在所有可能的持续时间内(1s,2s,3s ...)预先生成它们,然后投放适当的音频片段。

答案 1 :(得分:-1)

您可以执行以下操作:

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!"

from threading import Event as event
play="Y"
number=0
x=int(input("Audio length:"))
return start_audio()
for i in range(x):
  if play="Y":
    event().wait(1)
    number+=1
    if number==play:
      play="N"
  else:
    #insert way to stop audio here