Skip to content

Instantly share code, notes, and snippets.

@mludvig
Last active March 13, 2019 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mludvig/f5127c4bf7c809e69c51c5b73c75f14c to your computer and use it in GitHub Desktop.
Save mludvig/f5127c4bf7c809e69c51c5b73c75f14c to your computer and use it in GitHub Desktop.
Sample code for using AWS Polly in Python
#!/usr/bin/env python3
# audio_helper.py - simple PyGame audio player
# Author Michael Ludvig
import io
import pygame
# PyGame initialisation - upon module loading
pygame.init()
pygame.mixer.init()
# Convert boto3 audio stream to Bytes stream
# for compatibility with pygame
def play_audio_stream(audio_stream):
audio = io.BytesIO(audio_stream.read())
play_audio(audio)
# Here we play the audio stream
def play_audio(audio):
pygame.mixer.music.load(audio)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment