Search code examples
pythonamazon-web-servicesbotoboto3amazon-lex

How to get audio from Amazon Lex response using boto3?


I have an Amazon Lex instance setup in AWS.

I can communicate with it using the python library boto3

I can successfully get and send responses using the client.post_text() command as well as the client.post_content()

I can see that there is a audioStream object which is of type StreamingBody attached to the response, but I seem to have no way to play that stream. I have tried:

audio = response['audioStream']
audio.read()

but this seems to have no effect. There is very little documentation describing how to use this object.

How do I accomplish this?


Solution

  • The best solution I have found is the following, using pygame to parse the audio:

    I get the response from Lex and parse it out to find the audio stream, and then play it.

    Stream = response["audioStream"]
    
    os.environ["SDL_VIDEODRIVER"] = "dummy"
    pygame.init()
    pygame.display.set_mode((1,1))
    pygame.mixer.music.load(Stream)
    pygame.mixer.music.play(0)
    
    clock = pygame.time.Clock()
    clock.tick(10)
    while pygame.mixer.music.get_busy():
        pygame.event.poll()
        clock.tick(10)
    pygame.display.quit()
    pygame.quit()
    return
    

    Note: I set the video driver to "dummy" so I can run this on my device over ssh. Otherwise pygame requires an Xserver instance (its main use is to run a game after all). It also will not run in the background (a screen instance for example) if you're running Lex on something headless