我正在尝试构建可以播放音频文件的Alexa技能。我试图在启动请求中发送音频播放器播放指令,但是当我使用此代码时,我的Alexa没有回复。它看起来是否正确?
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
console.log('IN LAUNCHREQUEST');
return handlerInput.responseBuilder
.addDirective({
type: 'AudioPlayer.Play',
playBehavior: 'REPLACE_ALL',
audioItem: {
stream: {
token: "0",
url: "myurlhere",
offsetInMilliseconds: 0
}
}
})
}
};
答案 0 :(得分:0)
您必须在处理程序中返回“已构建”响应。因此,在您的情况下,代码将是:
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
console.log('IN LAUNCHREQUEST');
return handlerInput.responseBuilder
.addDirective({
type: 'AudioPlayer.Play',
playBehavior: 'REPLACE_ALL',
audioItem: {
stream: {
token: "0",
url: "myurlhere",
offsetInMilliseconds: 0
}
}
})
.getResponse();
// ^^^ add this line
}
};
答案 1 :(得分:0)
如果您正在使用alexa sdk v2(https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs),则可以使用内置方法播放音频。可以使用以下方法播放长格式音频。
addAudioPlayerPlayDirective(playBehavior: interfaces.audioplayer.PlayBehavior, url: string, token: string, offsetInMilliseconds: number, expectedPreviousToken?: string, audioItemMetadata? : AudioItemMetadata): this;
addAudioPlayerStopDirective(): this;
addAudioPlayerClearQueueDirective(clearBehavior: interfaces.audioplayer.ClearBehavior): this;
更多信息,请访问https://ask-sdk-for-nodejs.readthedocs.io/en/latest/Building-Response.html
以下是我在lambda中用来播放音频的代码段。
//Create Image to be displayed with song
const metadata = {
title: 'Stopwatch Audio',
art: {
sources: [{
url: imageUrl
}]
}
};
handlerInput.responseBuilder.speak(speechText).addAudioPlayerPlayDirective("REPLACE_ALL", audiofile, audiofile, 0, null, metadata).withShouldEndSession(true).getResponse();