Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var Alexa = require('alexa-sdk');
- var constants = {
- // App-ID. TODO: set to your own Skill App ID from the developer portal.
- appId : '[PRIVATE ID]',
- states : {
- START_MODE : '',
- PLAY_MODE : '_PLAY_MODE',
- RESUME_DECISION_MODE : '_RESUME_DECISION_MODE'
- }
- }
- /* **************************************************************** */
- var stateHandlers = {
- startModeIntentHandlers : Alexa.CreateStateHandler(constants.states.START_MODE, {
- /*
- * All Intent Handlers for state : START_MODE
- */
- 'LaunchRequest' : function () {
- // Initialize Attributes
- this.attributes['offsetInMilliseconds'] = 0;
- this.attributes['loop'] = true;
- // Change state to START_MODE
- this.handler.state = constants.states.START_MODE;
- controller.play.call(this);
- /*
- var message = 'You can say, play the audio, to begin.';
- var reprompt = 'You can say, play the audio, to begin.';
- this.response.speak(message).listen(reprompt);
- this.emit(':responseReady');
- */
- },
- 'PlayAudio' : function () {
- /*
- if (!this.attributes['playOrder']) {
- // Initialize Attributes if undefined.
- this.attributes['playOrder'] = Array.apply(null, {length: audioData.length}).map(Number.call, Number);
- this.attributes['index'] = 0;
- this.attributes['offsetInMilliseconds'] = 0;
- this.attributes['loop'] = true;
- this.attributes['shuffle'] = false;
- this.attributes['playbackIndexChanged'] = true;
- // Change state to START_MODE
- this.handler.state = constants.states.START_MODE;
- }
- controller.play.call(this);
- */
- this.attributes['offsetInMilliseconds'] = 0;
- this.attributes['loop'] = true;
- // Change state to START_MODE
- this.handler.state = constants.states.START_MODE;
- controller.play.call(this);
- },
- 'AMAZON.HelpIntent' : function () {
- var message = 'Welcome to the AWS Podcast. You can say, play the audio, to begin the podcast.';
- this.response.speak(message).listen(message);
- this.emit(':responseReady');
- },
- 'AMAZON.StopIntent' : function () {
- controller.stop.call(this);
- var message = 'Good bye.';
- this.response.speak(message);
- this.emit(':responseReady');
- },
- 'AMAZON.CancelIntent' : function () {
- controller.stop.call(this);
- var message = 'Good bye.';
- this.response.speak(message);
- this.emit(':responseReady');
- },
- 'SessionEndedRequest' : function () {
- // No session ended logic
- },
- 'Unhandled' : function () {
- var message = 'Sorry, I could not understand. Please say, play the audio, to begin the audio.';
- this.response.speak(message).listen(message);
- this.emit(':responseReady');
- }
- }),
- playModeIntentHandlers : Alexa.CreateStateHandler(constants.states.PLAY_MODE, {
- /*
- * All Intent Handlers for state : PLAY_MODE
- */
- 'LaunchRequest' : function () { controller.play.call(this) },
- 'PlayAudio' : function () { controller.play.call(this) },
- 'AMAZON.NextIntent' : function () { controller.playNext.call(this) },
- 'AMAZON.PreviousIntent' : function () { controller.playPrevious.call(this) },
- 'AMAZON.PauseIntent' : function () { controller.stop.call(this) },
- 'AMAZON.StopIntent' : function () { controller.stop.call(this) },
- 'AMAZON.CancelIntent' : function () { controller.stop.call(this) },
- 'AMAZON.ResumeIntent' : function () { controller.play.call(this) },
- 'AMAZON.LoopOnIntent' : function () { controller.loopOn.call(this) },
- 'AMAZON.LoopOffIntent' : function () { controller.loopOff.call(this) },
- 'AMAZON.ShuffleOnIntent' : function () { controller.shuffleOn.call(this) },
- 'AMAZON.ShuffleOffIntent' : function () { controller.shuffleOff.call(this) },
- 'AMAZON.StartOverIntent' : function () { controller.startOver.call(this) },
- 'AMAZON.HelpIntent' : function () {
- // This will called while audio is playing and a user says "ask <invocation_name> for help"
- var message = 'You are listening to forest sounds. You can say, Loop On to listen for as long as you\'d like.';
- this.response.speak(message).listen(message);
- this.emit(':responseReady');
- },
- 'SessionEndedRequest' : function () {
- // No session ended logic
- },
- 'Unhandled' : function () {
- var message = 'Sorry, I could not understand.';
- this.response.speak(message).listen(message);
- this.emit(':responseReady');
- }
- }),
- remoteControllerHandlers : Alexa.CreateStateHandler(constants.states.PLAY_MODE, {
- /*
- * All Requests are received using a Remote Control. Calling corresponding handlers for each of them.
- */
- 'PlayCommandIssued' : function () { controller.play.call(this) },
- 'PauseCommandIssued' : function () { controller.stop.call(this) },
- 'NextCommandIssued' : function () { controller.play.call(this) },
- 'PreviousCommandIssued' : function () { controller.play.call(this) }
- }),
- resumeDecisionModeIntentHandlers : Alexa.CreateStateHandler(constants.states.RESUME_DECISION_MODE, {
- /*
- * All Intent Handlers for state : RESUME_DECISION_MODE
- */
- 'LaunchRequest' : function () { controller.play.call(this) },
- 'AMAZON.YesIntent' : function () { controller.play.call(this) },
- 'AMAZON.NoIntent' : function () { controller.play.call(this) },
- 'AMAZON.HelpIntent' : function () {
- var message = 'You can ask me to loop, to enable continous playback';
- this.response.speak(message).listen(message);
- this.emit(':responseReady');
- },
- 'AMAZON.StopIntent' : function () {
- controller.stop.call(this);
- var message = 'Good bye.';
- this.response.speak(message);
- this.emit(':responseReady');
- },
- 'AMAZON.CancelIntent' : function () {
- controller.stop.call(this);
- var message = 'Good bye.';
- this.response.speak(message);
- this.emit(':responseReady');
- },
- 'SessionEndedRequest' : function () {
- // No session ended logic
- },
- 'Unhandled' : function () {
- var message = 'Sorry, this is not a valid command. Please say help to hear what you can say.';
- this.response.speak(message).listen(message);
- this.emit(':responseReady');
- }
- })
- };
- var audioEventHandlers = Alexa.CreateStateHandler(constants.states.PLAY_MODE, {
- 'PlaybackStarted' : function () {
- /*
- * AudioPlayer.PlaybackStarted Directive received.
- * Confirming that requested audio file began playing.
- * Storing details in dynamoDB using attributes.
- */
- this.attributes['token'] = getToken.call(this);
- this.attributes['index'] = getIndex.call(this);
- this.attributes['playbackFinished'] = false;
- },
- 'PlaybackFinished' : function () {
- /*
- * AudioPlayer.PlaybackFinished Directive received.
- * Confirming that audio file completed playing.
- * Storing details in dynamoDB using attributes.
- */
- this.attributes['playbackFinished'] = true;
- this.attributes['enqueuedToken'] = false;
- controller.stop.call(this);
- },
- 'PlaybackStopped' : function () {
- /*
- * AudioPlayer.PlaybackStopped Directive received.
- * Confirming that audio file stopped playing.
- * Storing details in dynamoDB using attributes.
- */
- this.attributes['token'] = getToken.call(this);
- this.attributes['index'] = getIndex.call(this);
- this.attributes['offsetInMilliseconds'] = getOffsetInMilliseconds.call(this);
- controller.stop.call(this);
- },
- 'PlaybackFailed' : function () {
- // AudioPlayer.PlaybackNearlyFinished Directive received. Logging the error.
- console.log("Playback Failed : %j", this.event.request.error);
- this.context.succeed(true);
- controller.stop.call(this);
- }
- });
- /* **************************************************************** */
- var controller = function () {
- return {
- play: function () {
- var playBehavior = 'REPLACE_ALL';
- // Since play behavior is REPLACE_ALL, enqueuedToken attribute need to be set to null.
- this.attributes['enqueuedToken'] = null;
- var audio = audioData[0];
- var token = audio.title;
- var offsetInMilliseconds = 0;
- // Since play behavior is REPLACE_ALL, enqueuedToken attribute need to be set to null.
- this.response.audioPlayerPlay(playBehavior, audio.url, token, null, offsetInMilliseconds);
- this.emit(':responseReady');
- },
- stop: function () {
- /*
- * Issuing AudioPlayer.Stop directive to stop the audio.
- * Attributes already stored when AudioPlayer.Stopped request received.
- */
- this.attributes['playbackFinished'] = true;
- this.response.audioPlayerStop();
- this.emit(':responseReady');
- },
- playNext: function () {
- this.attributes['offsetInMilliseconds'] = 0;
- controller.play.call(this);
- },
- playPrevious: function () {
- this.attributes['offsetInMilliseconds'] = 0;
- controller.play.call(this);
- },
- loopOn: function () {
- // Turn on loop play.
- this.attributes['loop'] = true;
- var message = 'Loop turned on.';
- this.response.speak(message);
- this.emit(':responseReady');
- },
- loopOff: function () {
- // Turn off looping
- this.attributes['loop'] = false;
- var message = 'Loop turned off.';
- this.response.speak(message);
- this.emit(':responseReady');
- },
- shuffleOn: function () {
- var message = 'Shuffle Not Supported.';
- this.response.speak(message);
- this.emit(':responseReady');
- },
- shuffleOff: function () {
- var message = 'Shuffle Not Supported.';
- this.response.speak(message);
- this.emit(':responseReady');
- },
- startOver: function () {
- // Start over the current audio file.
- this.attributes['offsetInMilliseconds'] = 0;
- controller.play.call(this);
- },
- reset: function () {
- // Reset to top of the playlist.
- this.attributes['offsetInMilliseconds'] = 0;
- controller.play.call(this);
- }
- }
- }();
- var audioData = [
- {
- 'title' : 'Forest Sounds',
- 'url' : '[PRIVATE URL]/Relaxing-forest-sounds.mp3'
- }
- ];
- exports.handler = function(event, context, callback){
- var alexa = Alexa.handler(event, context);
- console.log(event);
- console.log(context);
- alexa.appId = constants.appId;
- alexa.registerHandlers(
- stateHandlers.startModeIntentHandlers,
- stateHandlers.playModeIntentHandlers,
- stateHandlers.remoteControllerHandlers,
- stateHandlers.resumeDecisionModeIntentHandlers,
- audioEventHandlers
- );
- alexa.execute();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement