Advertisement
brombomb

ForestSounds

Jul 11th, 2017
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. var Alexa = require('alexa-sdk');
  4.  
  5. var constants = {
  6.         // App-ID. TODO: set to your own Skill App ID from the developer portal.
  7.     appId : '[PRIVATE ID]',
  8.    
  9.     states : {
  10.         START_MODE : '',
  11.         PLAY_MODE : '_PLAY_MODE',
  12.         RESUME_DECISION_MODE : '_RESUME_DECISION_MODE'
  13.     }
  14. }
  15.  
  16.  
  17. /* **************************************************************** */
  18.  
  19. var stateHandlers = {
  20.     startModeIntentHandlers : Alexa.CreateStateHandler(constants.states.START_MODE, {
  21.         /*
  22.          *  All Intent Handlers for state : START_MODE
  23.          */
  24.         'LaunchRequest' : function () {
  25.             // Initialize Attributes
  26.             this.attributes['offsetInMilliseconds'] = 0;
  27.             this.attributes['loop'] = true;
  28.             //  Change state to START_MODE
  29.             this.handler.state = constants.states.START_MODE;
  30.  
  31.             controller.play.call(this);
  32.             /*
  33.             var message = 'You can say, play the audio, to begin.';
  34.             var reprompt = 'You can say, play the audio, to begin.';
  35.  
  36.             this.response.speak(message).listen(reprompt);
  37.             this.emit(':responseReady');
  38.             */
  39.         },
  40.         'PlayAudio' : function () {
  41.             /*
  42.             if (!this.attributes['playOrder']) {
  43.                 // Initialize Attributes if undefined.
  44.                 this.attributes['playOrder'] = Array.apply(null, {length: audioData.length}).map(Number.call, Number);
  45.                 this.attributes['index'] = 0;
  46.                 this.attributes['offsetInMilliseconds'] = 0;
  47.                 this.attributes['loop'] = true;
  48.                 this.attributes['shuffle'] = false;
  49.                 this.attributes['playbackIndexChanged'] = true;
  50.                 //  Change state to START_MODE
  51.                 this.handler.state = constants.states.START_MODE;
  52.             }
  53.             controller.play.call(this);
  54.             */
  55.             this.attributes['offsetInMilliseconds'] = 0;
  56.             this.attributes['loop'] = true;
  57.             //  Change state to START_MODE
  58.             this.handler.state = constants.states.START_MODE;
  59.  
  60.             controller.play.call(this);
  61.         },
  62.         'AMAZON.HelpIntent' : function () {
  63.             var message = 'Welcome to the AWS Podcast. You can say, play the audio, to begin the podcast.';
  64.             this.response.speak(message).listen(message);
  65.             this.emit(':responseReady');
  66.         },
  67.         'AMAZON.StopIntent' : function () {
  68.             controller.stop.call(this);
  69.             var message = 'Good bye.';
  70.             this.response.speak(message);
  71.             this.emit(':responseReady');
  72.         },
  73.         'AMAZON.CancelIntent' : function () {
  74.             controller.stop.call(this);
  75.             var message = 'Good bye.';
  76.             this.response.speak(message);
  77.             this.emit(':responseReady');
  78.         },
  79.         'SessionEndedRequest' : function () {
  80.             // No session ended logic
  81.         },
  82.         'Unhandled' : function () {
  83.             var message = 'Sorry, I could not understand. Please say, play the audio, to begin the audio.';
  84.             this.response.speak(message).listen(message);
  85.             this.emit(':responseReady');
  86.         }
  87.     }),
  88.     playModeIntentHandlers : Alexa.CreateStateHandler(constants.states.PLAY_MODE, {
  89.        /*
  90.          *  All Intent Handlers for state : PLAY_MODE
  91.          */
  92.         'LaunchRequest' : function () { controller.play.call(this) },
  93.         'PlayAudio' : function () { controller.play.call(this) },
  94.         'AMAZON.NextIntent' : function () { controller.playNext.call(this) },
  95.         'AMAZON.PreviousIntent' : function () { controller.playPrevious.call(this) },
  96.         'AMAZON.PauseIntent' : function () { controller.stop.call(this) },
  97.         'AMAZON.StopIntent' : function () { controller.stop.call(this) },
  98.         'AMAZON.CancelIntent' : function () { controller.stop.call(this) },
  99.         'AMAZON.ResumeIntent' : function () { controller.play.call(this) },
  100.         'AMAZON.LoopOnIntent' : function () { controller.loopOn.call(this) },
  101.         'AMAZON.LoopOffIntent' : function () { controller.loopOff.call(this) },
  102.         'AMAZON.ShuffleOnIntent' : function () { controller.shuffleOn.call(this) },
  103.         'AMAZON.ShuffleOffIntent' : function () { controller.shuffleOff.call(this) },
  104.         'AMAZON.StartOverIntent' : function () { controller.startOver.call(this) },
  105.         'AMAZON.HelpIntent' : function () {
  106.             // This will called while audio is playing and a user says "ask <invocation_name> for help"
  107.             var message = 'You are listening to forest sounds. You can say, Loop On to listen for as long as you\'d like.';
  108.             this.response.speak(message).listen(message);
  109.             this.emit(':responseReady');
  110.         },
  111.         'SessionEndedRequest' : function () {
  112.             // No session ended logic
  113.         },
  114.         'Unhandled' : function () {
  115.             var message = 'Sorry, I could not understand.';
  116.             this.response.speak(message).listen(message);
  117.             this.emit(':responseReady');
  118.         }
  119.     }),
  120.     remoteControllerHandlers : Alexa.CreateStateHandler(constants.states.PLAY_MODE, {
  121.         /*
  122.          *  All Requests are received using a Remote Control. Calling corresponding handlers for each of them.
  123.          */
  124.         'PlayCommandIssued' : function () { controller.play.call(this) },
  125.         'PauseCommandIssued' : function () { controller.stop.call(this) },
  126.         'NextCommandIssued' : function () { controller.play.call(this) },
  127.         'PreviousCommandIssued' : function () { controller.play.call(this) }
  128.     }),
  129.     resumeDecisionModeIntentHandlers : Alexa.CreateStateHandler(constants.states.RESUME_DECISION_MODE, {
  130.         /*
  131.          *  All Intent Handlers for state : RESUME_DECISION_MODE
  132.          */
  133.         'LaunchRequest' : function () { controller.play.call(this) },
  134.         'AMAZON.YesIntent' : function () { controller.play.call(this) },
  135.         'AMAZON.NoIntent' : function () { controller.play.call(this) },
  136.         'AMAZON.HelpIntent' : function () {
  137.             var message = 'You can ask me to loop, to enable continous playback';
  138.             this.response.speak(message).listen(message);
  139.             this.emit(':responseReady');
  140.         },
  141.         'AMAZON.StopIntent' : function () {
  142.             controller.stop.call(this);
  143.             var message = 'Good bye.';
  144.             this.response.speak(message);
  145.             this.emit(':responseReady');
  146.         },
  147.         'AMAZON.CancelIntent' : function () {
  148.             controller.stop.call(this);
  149.             var message = 'Good bye.';
  150.             this.response.speak(message);
  151.             this.emit(':responseReady');
  152.         },
  153.         'SessionEndedRequest' : function () {
  154.             // No session ended logic
  155.         },
  156.         'Unhandled' : function () {
  157.             var message = 'Sorry, this is not a valid command. Please say help to hear what you can say.';
  158.             this.response.speak(message).listen(message);
  159.             this.emit(':responseReady');
  160.         }
  161.     })
  162. };
  163.  
  164. var audioEventHandlers = Alexa.CreateStateHandler(constants.states.PLAY_MODE, {
  165.     'PlaybackStarted' : function () {
  166.         /*
  167.          * AudioPlayer.PlaybackStarted Directive received.
  168.          * Confirming that requested audio file began playing.
  169.          * Storing details in dynamoDB using attributes.
  170.          */
  171.         this.attributes['token'] = getToken.call(this);
  172.         this.attributes['index'] = getIndex.call(this);
  173.         this.attributes['playbackFinished'] = false;
  174.     },
  175.     'PlaybackFinished' : function () {
  176.         /*
  177.          * AudioPlayer.PlaybackFinished Directive received.
  178.          * Confirming that audio file completed playing.
  179.          * Storing details in dynamoDB using attributes.
  180.          */
  181.         this.attributes['playbackFinished'] = true;
  182.         this.attributes['enqueuedToken'] = false;
  183.         controller.stop.call(this);
  184.     },
  185.     'PlaybackStopped' : function () {
  186.         /*
  187.          * AudioPlayer.PlaybackStopped Directive received.
  188.          * Confirming that audio file stopped playing.
  189.          * Storing details in dynamoDB using attributes.
  190.          */
  191.         this.attributes['token'] = getToken.call(this);
  192.         this.attributes['index'] = getIndex.call(this);
  193.         this.attributes['offsetInMilliseconds'] = getOffsetInMilliseconds.call(this);
  194.         controller.stop.call(this);
  195.     },
  196.     'PlaybackFailed' : function () {
  197.         //  AudioPlayer.PlaybackNearlyFinished Directive received. Logging the error.
  198.         console.log("Playback Failed : %j", this.event.request.error);
  199.         this.context.succeed(true);
  200.         controller.stop.call(this);
  201.     }
  202. });
  203.  
  204. /* **************************************************************** */
  205.  
  206.  
  207. var controller = function () {
  208.     return {
  209.         play: function () {
  210.  
  211.             var playBehavior = 'REPLACE_ALL';
  212.             // Since play behavior is REPLACE_ALL, enqueuedToken attribute need to be set to null.
  213.             this.attributes['enqueuedToken'] = null;
  214.            
  215.             var audio = audioData[0];
  216.             var token = audio.title;
  217.  
  218.             var offsetInMilliseconds = 0;
  219.             // Since play behavior is REPLACE_ALL, enqueuedToken attribute need to be set to null.
  220.             this.response.audioPlayerPlay(playBehavior, audio.url, token, null, offsetInMilliseconds);
  221.             this.emit(':responseReady');
  222.         },
  223.         stop: function () {
  224.             /*
  225.              *  Issuing AudioPlayer.Stop directive to stop the audio.
  226.              *  Attributes already stored when AudioPlayer.Stopped request received.
  227.              */
  228.             this.attributes['playbackFinished'] = true;
  229.             this.response.audioPlayerStop();
  230.             this.emit(':responseReady');
  231.         },
  232.         playNext: function () {
  233.             this.attributes['offsetInMilliseconds'] = 0;
  234.             controller.play.call(this);
  235.         },
  236.         playPrevious: function () {
  237.             this.attributes['offsetInMilliseconds'] = 0;
  238.             controller.play.call(this);
  239.         },
  240.         loopOn: function () {
  241.             // Turn on loop play.
  242.             this.attributes['loop'] = true;
  243.             var message = 'Loop turned on.';
  244.             this.response.speak(message);
  245.             this.emit(':responseReady');
  246.         },
  247.         loopOff: function () {
  248.             // Turn off looping
  249.             this.attributes['loop'] = false;
  250.             var message = 'Loop turned off.';
  251.             this.response.speak(message);
  252.             this.emit(':responseReady');
  253.         },
  254.         shuffleOn: function () {
  255.             var message = 'Shuffle Not Supported.';
  256.             this.response.speak(message);
  257.             this.emit(':responseReady');
  258.         },
  259.         shuffleOff: function () {
  260.             var message = 'Shuffle Not Supported.';
  261.             this.response.speak(message);
  262.             this.emit(':responseReady');
  263.         },
  264.         startOver: function () {
  265.             // Start over the current audio file.
  266.             this.attributes['offsetInMilliseconds'] = 0;
  267.             controller.play.call(this);
  268.         },
  269.         reset: function () {
  270.             // Reset to top of the playlist.
  271.             this.attributes['offsetInMilliseconds'] = 0;
  272.             controller.play.call(this);
  273.         }
  274.     }
  275. }();
  276.  
  277.  
  278. var audioData = [
  279.     {
  280.         'title' : 'Forest Sounds',
  281.         'url' : '[PRIVATE URL]/Relaxing-forest-sounds.mp3'
  282.     }
  283. ];
  284.  
  285. exports.handler = function(event, context, callback){
  286.     var alexa = Alexa.handler(event, context);
  287.     console.log(event);
  288.     console.log(context);
  289.     alexa.appId = constants.appId;
  290.     alexa.registerHandlers(
  291.         stateHandlers.startModeIntentHandlers,
  292.         stateHandlers.playModeIntentHandlers,
  293.         stateHandlers.remoteControllerHandlers,
  294.         stateHandlers.resumeDecisionModeIntentHandlers,
  295.         audioEventHandlers
  296.     );
  297.     alexa.execute();
  298. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement