大家好,所以我在尝试实现音频播放时遇到了困难。Here's the docs
所以我真正想做的事情看起来很简单,但结果却很混乱,一切都应该去。
我希望能够说出命令。Alexa 将响应一点 outputSpeech,然后继续播放我将提供的小型音轨 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
现在一切正常,亚马逊可以回复我并结束会话。
我怎样才能让亚马逊回复我,然后播放一些音频?这些文档有点不适合我。