亚马逊Echo在命令中播放音乐

时间:2016-09-22 20:51:00

标签: javascript audio alexa-skills-kit amazon-echo

大家好,所以我很难尝试实现音频播放。这是docs

所以我真正想要做的事情看起来很简单,但在一切都被假设出现的时候,它变得非常混乱。

我希望能够说出一个命令。 Alexa会响应一点输出声音,然后继续播放我将提供的小型音轨mp3。我不介意在本地上传它(当我压缩文件并将它们导入lamda函数时)或使用S3 Buckets SDK来传输mp3文件。这对你们来说更容易。

这是我到目前为止所得到的。

通过以下代码,我可以让Alexa回应语音并输出语音。

我只使用IntentRequest为您减少代码。

  • 我要说" Alexa打开myApp并播放我的音乐"
  • "播放我的音乐"当我在alexa开发者控制台中设置我的技能时,我将作为我的话语列出的命令
exports.handler = (event, context, callback) => {
    try {
        if (event.request.type === 'IntentRequest') {
            onIntent(event.request,
                event.session,
                (sessionAttributes, speechletResponse) => {
                    callback(null, buildResponse(sessionAttributes, speechletResponse));
                });
        }
    } catch (err) {
        callback(err);
    }
};


我的功能将在Intent请求通过时调用

  • 我的意图名称是 PlayMyMusic


function onIntent(intentRequest, session, callback) {
    console.log(`onIntent requestId=${intentRequest.requestId}, sessionId=${session.sessionId}`);

    const intent = intentRequest.intent;
    const intentName = intentRequest.intent.name;

    if (intentName === 'PlayMyMusic') {
        PlayMyMusic(intent, session, callback);
    } else if (intentName === 'AMAZON.StopIntent' || intentName === 'AMAZON.CancelIntent') {
        handleSessionEndRequest(callback);
    } else {
        throw new Error('Invalid intent');
    }
}


这是输出消息

function PlayMyMusic(intent, session, callback) {

    const repromptText = null;
    const sessionAttributes = {};
    let shouldEndSession = true;
    let speechOutput = '';

        speechOutput = `I'm Alexa and I will output speech in this area. After I'm done talking I will play an audio track`;

    callback(sessionAttributes,
        buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession));
}

这是我简单的意图模式

{
  "intents": [
    {
      "intent": "PlayMyMusic"
    },
    {
      "intent": "AMAZON.HelpIntent"
   }
  ]
}

示例话语

PlayMyMusic play my music


现在一切正常,亚马逊可以回复并结束会话。

我怎样才能让亚马逊回应我,然后播放一些音频?这些文档对我不起作用。

  • 我在哪里放置播放指令? (AudioPlayer.Play)
  • 1 个答案:

    答案 0 :(得分:1)

    您可以使用SSML并在任何https网址添加mp3路径,它将播放歌曲

    see this

    String