2017-06-21 17 views
1

AWS LambdaのJavascriptでコードを作成しようとしています。このコードは、AlexaがURLに行き、そこでAudioPlayerを使用してオーディオをストリーミングすることを目的としています。 このコードで何が欠けているのか、何が間違っているのか分からず、ログからこのエラーが発生します。AWS Lambda Javascript for AudioPlayer

コード:

'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": "モジュール 'アレクサ-SDK' を見つけることができません"、 "ERRORTYPE": "エラー"、 "スタックトレース":[ "function.Module._load(module.js:417:25)"、 "Module.require(module.js:497:17)"、 "require(internal/module.js:20:19)"、 "オブジェクト(/ var/task/inde (Module.js:579:10) "、 " Module._compile(module.js:2:13) "、 " Module._compile(module.js:570:32) "、 " Object.Module._extensions..js (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; 
 
};