适用于AudioPlayer的AWS Lambda Javascript

时间:2017-06-21 18:51:04

标签: javascript amazon-web-services aws-lambda

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

代码:

' '.join(l).partition('but')[-1].split() # ['nothing', 'to', 'do']

1 个答案:

答案 0 :(得分:0)

我最终改变了我的代码。

代码:

var lastPlayedByUser = {};
var streamURL = "http://cpdc101-lh.akamaihd.net/i/ISNCPDCMB1_1@314337/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;
};