2017-06-21 21 views
1

尝试使用AWS Lambda中的Javascript代码进行编码。该代码旨在让Alexa转到一个URL并使用AudioPlayer在那里传输音频。 无法弄清楚我在这段代码中缺少什么,或者它有什么问题,我通过日志得到这个错误。适用于AudioPlayer的AWS Lambda Javascript

代码:

'use strict'; 
 

 
var alexa = require('alexa-sdk'); 
 

 
var APP_ID = "amzn1.ask.skill.b5c95058-7134-4044-9e77-a4279e0adaf7"; 
 

 
var PAUSE_MESSAGE = "paused!"; 
 
var RESUME_MESSAGE = "resumed!"; 
 

 
exports.handler = function(event, context, callback) { 
 
    var alexa = Alexa.handler(event, context); 
 
    alexa.APP_ID = APP_ID; 
 
    alexa.registerHandlers(handlers); 
 
    alexa.execute(); 
 
}; 
 

 
var handlers = { 
 
    'play': function(audioURL, offsetInMilliseconds) { 
 
    var response = { 
 
     version: "1.0", 
 
     response: { 
 
     shouldEndSession: true, 
 
     directives: [{ 
 
      type: "AudioPlayer.Play", 
 
      playBehavior: "REPLACE_ALL", 
 
      audioItem: { 
 
      stream: { 
 
       url: 'https://feeds.soundcloud.com/stream/275202399-amazon-web-services-306355661-amazon-web-services.mp3', 
 
       offsetInMilliseconds: 10 
 
      } 
 
      } 
 
     }] 
 
     } 
 
    } 
 
    this.context.succeed(response); 
 
    }, 
 
    'AMAZON.PauseIntent': function() { 
 
    this.emit(':tell', PAUSE_MESSAGE); 
 
    }, 
 
    'AMAZON.ResumeIntent': function() { 
 
    this.emit(':tell', RESUME_MESSAGE); 
 
    } 
 
};

+0

{ “的errorMessage”: “找不到模块 '的alexa-SDK'”, “ERRORTYPE”: “错误”, “堆栈跟踪”: “要求(内部/模块.js:20:19)”的“Module.require(module.js:497: “Object。(/ var/task/inde x.js:2:13)“, ”Module._compile(module.js:570:32)“, ”Object.Module._extensions..js(module.js:579:10)“, ”Module .load(module.js:487:32)”, “tryModuleLoad(module.js:446:12)”, “Function.Module._load(module.js:438:3)” ] } –

+0

HTTPS ://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/ – Will

回答

0

我最终改变我的代码。

代码:

var lastPlayedByUser = {}; 
 
var streamURL = "http://cpdc101-lh.akamaihd.net/i/[email protected]/master.m3u8"; 
 

 

 
exports.handler = function(event, context) { 
 
    var player = new Player(event, context); 
 
    player.handle(); 
 
}; 
 

 
var Player = function (event, context) { 
 
    this.event = event; 
 
    this.context = context; 
 
}; 
 

 
Player.prototype.handle = function() { 
 
    var requestType = this.event.request.type; 
 
    var userId = this.event.context ? this.event.context.System.user.userId : this.event.session.user.userId; 
 

 
    if (requestType === "LaunchRequest") { 
 
     this.play(streamURL, 0); 
 

 
    } else if (requestType === "IntentRequest") { 
 
     var intent = this.event.request.intent; 
 
     if (intent.name === "Play") { 
 
      this.play(streamURL, 0); 
 

 
     } else if (intent.name === "AMAZON.PauseIntent") { 
 
      this.stop(); 
 

 
     } else if (intent.name === "AMAZON.ResumeIntent") { 
 
      var lastPlayed = this.loadLastPlayed(userId); 
 
      var offsetInMilliseconds = 0; 
 
      if (lastPlayed !== null) { 
 
       offsetInMilliseconds = lastPlayed.request.offsetInMilliseconds; 
 
      } 
 

 
      this.play(streamURL, offsetInMilliseconds); 
 
     } 
 
    } else if (requestType === "AudioPlayer.PlaybackStopped") { 
 
     this.saveLastPlayed(userId, this.event); 
 
     this.context.succeed(true); 
 
    } 
 
}; 
 

 

 
Player.prototype.play = function (audioURL, offsetInMilliseconds) { 
 
    var response = { 
 
     version: "1.0", 
 
     response: { 
 
      shouldEndSession: true, 
 
      directives: [ 
 
       { 
 
        type: "AudioPlayer.Play", 
 
        playBehavior: "REPLACE_ALL", 
 
        audioItem: { 
 
         stream: { 
 
          url: audioURL, 
 
          token: "0", 
 
          expectedPreviousToken: null, 
 
          offsetInMilliseconds: offsetInMilliseconds 
 
         } 
 
        } 
 
       } 
 
      ] 
 
     } 
 
    }; 
 

 
    this.context.succeed(response); 
 
}; 
 

 

 
Player.prototype.stop = function() { 
 
    var response = { 
 
     version: "1.0", 
 
     response: { 
 
      shouldEndSession: true, 
 
      directives: [ 
 
       { 
 
        type: "AudioPlayer.Stop" 
 
       } 
 
      ] 
 
     } 
 
    }; 
 

 
    this.context.succeed(response); 
 
}; 
 

 
Player.prototype.saveLastPlayed = function (userId, lastPlayed) { 
 
    lastPlayedByUser[userId] = lastPlayed; 
 
}; 
 

 
Player.prototype.loadLastPlayed = function (userId) { 
 
    var lastPlayed = null; 
 
    if (userId in lastPlayedByUser) { 
 
     lastPlayed = lastPlayedByUser[userId]; 
 
    } 
 
    return lastPlayed; 
 
};

相关问题