我正在使用 Alexa 技能包的 ask-sdk-core。我有以下 LaunchRequestHandler 和 AudioIntentHandler。我遇到的问题是,当我通过说“Alexa,与 SkillName 交谈”或“Alexa,开始 SkillName”来触发技能时,请求以某种方式发送 AudioIntent 而不是 LaunchRequest 到 lambda 函数,因此,音频流开始播放. 我能够通过开发者控制台触发正常行为和错误行为。
const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},
handle(handlerInput) {
const speakOutput = 'This message should be replied if the user launches the skill';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.addAudioPlayerStopDirective()
.getResponse();
}
};
以及其他几个 IntentHandler。这些 IntentHandler 之一启动音频流。
const AudioIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AudioIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.NextIntent'
|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.PreviousIntent');
},
handle(handlerInput) {
// this code should only be triggered if an utterance of the intent has been said and
// not on launch
const streamId = crypto.randomBytes(16).toString("hex");
return handlerInput.responseBuilder
.addAudioPlayerPlayDirective('REPLACE_ALL', mediaPath, streamId, 0, null, null)
.withShouldEndSession(true)
.getResponse();
}
};
音频意图处理程序由 ResumeIntentHandler 支持
const AudioResumeIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.ResumeIntent';
},
handle(handlerInput) {
// this code should only be triggered if an utterance of the intent has been said and
// not on launch
const streamId = crypto.randomBytes(16).toString("hex");
return handlerInput.responseBuilder
.addAudioPlayerPlayDirective('REPLACE_ALL', mediaPath, streamId, 0, null, null)
.withShouldEndSession(true)
.getResponse();
}
};
以及以下 AudioPlayer 处理程序
const AudioRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope).includes('AudioPlayer.');
},
handle(handlerInput) {
console.log(`AudioRequestHandler: ${handlerInput.requestEnvelope}`)
if(Alexa.getRequestType(handlerInput.requestEnvelope) === 'AudioPlayer.PlaybackFailed') {
console.log("Playback Failed : " + JSON.stringify(handlerInput.requestEnvelope.request.error, null, 2));
return handlerInput.responseBuilder
.speak('Something went wrong')
.getResponse();
}
return handlerInput.responseBuilder
.getResponse();
}
};
该技能按预期操作,例如 Aelxa,请向 SkillName 寻求帮助。我能够达到我所有的非音频意图。还可以帮助意图、取消、停止等按预期工作。为什么启动时会触发音频意图?
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
AudioRequestHandler,
AudioPauseIntentHandler,
AudioResumeIntentHandler,
NormalReplyIntentHandler,
AudioIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
FallbackIntentHandler,
SessionEndedRequestHandler,
IntentReflectorHandler)
.addErrorHandlers(
ErrorHandler)
.lambda();
我添加了更好的日志记录。错误行为似乎发生在用户第二次启动该技能之后。第一个技能交互效果很好。之后,“start SkillName”触发“AudioIntentHandler”。请求如下所示:
type: IntentRequest
intent: {
name: AudioIntent
confirmationStatus: NONE
}
所以在我看来,我的 addRequestHandlers 不是这里的问题,因为它的 Alexa 发送了错误的意图。AudioIntentHandler 根据错误请求正确触发。
错误请求包括 AudioPlayer 信息。可能来自与该技能的最后一次(第一次交互)。我相信这可能是错误的根源?
AudioPlayer {
offsetInMilliseconds: 3239,
playerActivity: STOPPED,
}
如果用户启动技能并且 Alexa 检测到存在停止的音频,Alexa 是否会继续使用音频意图?我在暂停或停止时没有正确清除音频播放器吗?
const AudioPauseIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.PauseIntent';
},
handle(handlerInput) {
console.log('AudioPauseIntentHandler')
const speakOutput = 'Audio stopped.';
return handlerInput.responseBuilder
.speak(speakOutput)
.addAudioPlayerStopDirective()
.addAudioPlayerClearQueueDirective('CLEAR_ALL')
.getResponse();
}
};