Skip to content

Instantly share code, notes, and snippets.

@Ravenstine
Last active April 12, 2017 20:16
Show Gist options
  • Save Ravenstine/9ed11b753aff371e56e90394cf7f7e38 to your computer and use it in GitHub Desktop.
Save Ravenstine/9ed11b753aff371e56e90394cf7f7e38 to your computer and use it in GitHub Desktop.
Playing audio through Alexa/Echo is easy!
// This is how I was able to get an mp3 file to play through my Echo Dot.
//
// Most audioPlayer examples seem overcomplicated, but this is essentially
// all I had to do. I did not test pausing, but you can say "Alexa, stop"
// and the audio will quit.
//
// Thought this would be helpful for those having trouble with this and
// will prove useful for those who just want to play audio clips without
// a complicated "interface".
//
// When you set up this skill in Lambda, set your Alexa app ID as the
// environment variable APP_ID.
//
'use strict';
const Alexa = require('alexa-sdk');
const languageStrings = {
'en-GB': {
translation: {
SKILL_NAME: 'Play Audio',
PLAY_AUDIO_MESSAGE: "Okay. Listen to this: ",
HELP_MESSAGE: 'You can ask me to play audio, or, you can say exit.',
HELP_REPROMPT: 'What can I help you with?',
STOP_MESSAGE: 'Cheerio!',
FAIL_MESSAGE: "There is a problem."
},
},
'en-US': {
translation: {
SKILL_NAME: 'Play Audio',
PLAY_AUDIO_MESSAGE: "Okay. Listen to this: ",
HELP_MESSAGE: 'You can ask me to play audio, or, you can say exit.',
HELP_REPROMPT: 'What can I help you with?',
STOP_MESSAGE: 'Goodbye!',
FAIL_MESSAGE: "There is a problem."
},
},
'de-DE': {
translation: {
SKILL_NAME: 'Audio abspielen',
PLAY_AUDIO_MESSAGE: "Okay. Hör zu: ",
HELP_MESSAGE: 'Sie können mich bitten, Audio zu spielen, oder, können Sie sagen, Ausfahrt.',
HELP_REPROMPT: 'Womit kann ich dir helfen?',
STOP_MESSAGE: 'Auf Wiedersehen!',
FAIL_MESSAGE: "Es gibt ein Problem."
},
},
};
const handlers = {
'LaunchRequest': function () {
this.emit('PlayAudio');
},
'PlayAudioIntent': function () {
this.emit('PlayAudio');
},
'PlayAudio': function () {
let url = "http://edge6-audio.wnyc.org/radiolab_podcast/radiolab_podcast16bringinggammaback.mp3";
this.response.speak(this.t('PLAY_AUDIO_MESSAGE'));
// playBehavior, url, enqueueToken, expectedPreviousToken, offsetInMilliseconds
this.response.audioPlayerPlay('REPLACE_ALL', url, 'radiolab_podcast16bringinggammaback.mp3', null, 0);
this.emit(':responseReady');
},
'AMAZON.HelpIntent': function () {
const speechOutput = this.t('HELP_MESSAGE');
const reprompt = this.t('HELP_MESSAGE');
this.emit(':ask', speechOutput, reprompt);
},
'AMAZON.CancelIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'AMAZON.StopIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'SessionEndedRequest': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
};
exports.handler = (event, context) => {
const alexa = Alexa.handler(event, context);
alexa.APP_ID = process.env.APP_ID;
// To enable string internationalization (i18n) features, set a resources object.
alexa.resources = languageStrings;
alexa.registerHandlers(handlers);
alexa.execute();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment