Amazon Echo 按命令播放音乐

Amazon Echo Playing Music on Command

大家好,我在尝试实现音频播放时遇到了困难。这是 docs

所以我真正想做的事情看起来很简单,但结果却很混乱。

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

这是我到目前为止得到的结果。

使用下面的代码,我可以让 Alexa 响应语音并输出语音。

我使用IntentRequest只是为了给你们减少代码。

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 请求通过时将调用我的函数


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));
}

这是我的简单 Intent Schema

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

示例话语

PlayMyMusic play my music


目前一切正常,Amazon 可以回复我并结束会话。

我怎样才能让亚马逊回复我然后播放一些音频?这些文档有点不适合我。

  • 播放指令放在哪里? (AudioPlayer.Play)
  • 您可以使用 SSML 并在任何 https url 添加 mp3 路径,它将播放歌曲

    see this

    Include the audio tag within your text-to-speech response within the speak tag. Alexa plays the MP3 at the specified point within the text to speech. For example:
    <speak>
        Welcome to Car-Fu. 
        <audio src="soundbank://soundlibrary/transportation/amzn_sfx_car_accelerate_01" /> 
        You can order a ride, or request a fare estimate. 
        Which will it be?
    </speak> 
    When Alexa renders this response, it would sound like this:
    Alexa: Welcome to Car-Fu.
    (the specified amzn_sfx_car_accelerate_01.mp3 audio file plays)
    Alexa: You can order a ride, or request a fare estimate. Which will