Alexa skill/Lambda - simple and easy way to play sounds using a lambda function

When I first googled how to do this I found a lot of overcomplicated answers. All I wanted to do was to play a bit of sound. I later found out this was quite easy! My examples are using node.js together with Lambda.

How to play sounds

The easiest way to play a piece of audio is to use the audio tag and just use Alexa's speech output. A simple example would be <audio src='yoursource' />. Where the src contains an url to a mp3 file. Below is an example with Alexa playing a sheep sound:

this.response.speak("<audio src='https://s3.amazonaws.com/ask-soundlibrary/animals/amzn_sfx_sheep_baa_01.mp3'/>";
this.emit(':responseReady');

You can find many more sounds at Alexa Skills kit sound library.

You can even add text before the audio. As the audio tag is part of Alexa's Speech Synthesis Markup Language:

this.response.speak("The sheep says: <audio src='https://s3.amazonaws.com/ask-soundlibrary/animals/amzn_sfx_sheep_baa_01.mp3'/>";
this.emit(':responseReady');

Which means you can also play several sounds after another.

Limitations

However there are some limitations to the audiotag:

  • The sound can be no longer than 240 seconds.
  • The bit rate must be 48 kbps, and sample rate at 22050Hz, 24000Hz, or 16000Hz.
  • Requires the audio file to be of the format MP3
  • The domain of the audio file must be https and present a valid, trusted SSL certificate. Self-signed certificates cannot be used.

The requirements can also be seen here

That's it!

That was my short introduction to the audio tag. I hope this helps someone out there :)

Do you not yet have an echo? You can easily order one from amazon: