Build Talking Apps for Alexa by Craig Walls

Build Talking Apps for Alexa by Craig Walls

Author:Craig Walls [Craig Walls]
Language: eng
Format: epub
Publisher: Pragmatic Bookshelf
Published: 2022-04-25T00:00:00+00:00


Playing Music and Sound Effects

As we saw in the previous chapter, using sound effects and music in a response adds some flair beyond just speech. APL-A’s Audio component serves that purpose, as shown here:

​ {

​ ​"type"​: ​"APLA"​,

​ ​"version"​: ​"0.9"​,

​ ​"mainTemplate"​: {

​ ​"item"​: {

​ ​"type"​: ​"Audio"​,

​ ​"source"​:

​ ​"soundbank://soundlibrary/scifi/amzn_sfx_scifi_small_zoom_flyby_01"​

​ }

​ }

​ }

Here, the source property is set to reference one of the many sounds in the ASK sound library.

Although the ASK sound library has a vast selection of sound effects, you can also provide your own by supplying a URL to an MP3 file in the source property, as shown in the following APL-A template:

​ {

​ ​"type"​: ​"APLA"​,

​ ​"version"​: ​"0.9"​,

​ ​"mainTemplate"​: {

​ ​"item"​: {

​ ​"type"​: ​"Audio"​,

​ ​"source"​: ​"https://starport75.dev/audio/SP75.mp3"​

​ }

​ }

​ }

It’s important that the given URL is accessible by the fulfillment code that backs your Alexa skill. It also must be an HTTPS URL because HTTP URLs will not work.

If you’re hosting your audio files in Amazon S3, then you’ll either need to open up the security on the S3 bucket to allow access to the audio files or, even better, leave them locked down, but use a pre-signed S3 URL. To help you obtain a pre-signed URL, the util.js module that you got when initializing the skill project provides a getS3PreSignedUrl() function.

While you can’t use the getS3PreSignedUrl() function in the APL-A template itself, you can call it from your fulfillment code and then pass the resulting URL as a parameter to the APL-A template.

We’ll look at how to pass parameters from the fulfillment code in section ​Returning APL-A Responses​. But to give some idea of how you might pass a pre-signed S3 URL to the APL-A template, consider the following snippet of fulfillment code:

​ ​const​ Util = require(​'./util'​);

​

​ ...

​

​ ​const​ LaunchRequestHandler = {

​ canHandle(handlerInput) { ... },

​ handle(handlerInput) {

​ ...

​ ​const​ mySoundUrl = Util.getS3PreSignedUrl(​'Media/sounds/my-sound.mp3'​);

​ ​return​ responseBuilder

​ ​ ​ .addDirective({

​ ​ ​​"type"​: ​"Alexa.Presentation.APLA.RenderDocument"​,

​ ​ ​​"document"​: {

​ ​ ​​"type"​: ​"Link"​,

​ ​ ​​"src"​: ​"doc://alexa/apla/documents/welcome"​

​ ​ ​},

​ ​"datasources"​: {

​ ​"sounds"​: {

​ ​"mySoundUrl"​: mySoundUrl

​ }

​ }

​ ​ ​})

​ .getResponse()

​ }

​ };

The URL returned from getS3PreSignedUrl() includes token information to get through the S3 bucket’s security. But it is only valid for about one minute, which is plenty of time for the response to be sent to the device. Do not try to assign it to a module-level constant, though, because it will expire before it can be used.

You may be wondering why you’d use the Audio component instead of just using the Speech component to return SSML with the <audio> tag. While you could do that, it limits how the audio is presented in relation to speech and other sounds. As we’ll see in section ​Mixing Sounds​, the Audio component can be mixed with Speech or other Audio components such that they play at the same time, effectively offering background audio.

But before we get to that, let’s take a quick look at the silent partner of the Speech and Audio components: the Silence component.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.