Skip to content

Instantly share code, notes, and snippets.

@germanviscuso
Last active February 24, 2020 16:02
Show Gist options
  • Save germanviscuso/e490afd1684f3a1216538762da0a882c to your computer and use it in GitHub Desktop.
Save germanviscuso/e490afd1684f3a1216538762da0a882c to your computer and use it in GitHub Desktop.
Quick code to support an intent that plays long form audio in an Alexa skill
var stream = {
"token": "my_token", // no auth token, you decide what this is
"url": 'https://my_song.mp3',
"metadata" : {
"title": "My Song Title",
"subtitle": "My Song Subtitle",
"art": {
"sources": [
{
"contentDescription": "image",
"url": "https://my_song_image.png",
"widthPixels": 512,
"heightPixels": 512
}
]
},
"backgroundImage": {
"sources": [
{
"contentDescription": "example image",
"url": "https://my_song_background_image.gif",
"widthPixels": 1200,
"heightPixels": 800
}
]
}
}
};
const SongHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return (request.type === 'IntentRequest'
&& request.intent.name === 'SongIntent') ||
(request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.ResumeIntent');
},
handle(handlerInput) {
handlerInput.responseBuilder
.speak(`starting ${stream.metadata.title}`)
.addAudioPlayerPlayDirective('REPLACE_ALL', stream.url, stream.token, 0, null, stream.metadata)
.getResponse();
}
};
const ExitHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& (
request.intent.name === 'AMAZON.CancelIntent'
|| request.intent.name === 'AMAZON.StopIntent'
|| request.intent.name === 'AMAZON.PauseIntent'
);
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak('bye bye')
.addAudioPlayerClearQueueDirective('CLEAR_ALL')
.addAudioPlayerStopDirective()
.getResponse();
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment