Alexa Skill NodeJS-结合语音和addAudioPlayerPlayDirective

时间:2019-01-11 15:31:57

标签: node.js alexa-skills-kit alexa-sdk-nodejs

我希望能够执行以下操作:

  1. 让alexa说些什么
  2. 播放音频文件
  3. 让alexa说些别的话

我尝试了以下无效的代码:

const IntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === "IntentRequest" &&
      handlerInput.requestEnvelope.request.intent.name === "MyIntent";
  },
  handle(handlerInput) {
    return handlerInput.responseBuilder
      .speak("Say something")
      .addAudioPlayerPlayDirective('REPLACE_ALL', audioFile, 'token', 0)
      .speak("Say something else")
      .getResponse();
  }
}

上面的代码的结果是这样的:

  1. “说点别的话”
  2. 音频文件播放

我该如何实现?

1 个答案:

答案 0 :(得分:0)

我通过使用ssml-builder包创建了SSML字符串并修改了用该字符串返回的响应来解决了这个问题。

const AmazonSpeech = require('ssml-builder/amazon_speech');
const speech = new AmazonSpeech();
speech.say('Start of the story')
  .audio(audioFile)
  .say('Finish the story');
const ssml = speech.ssml();
const response = handlerInput.responseBuilder.getResponse();
response.outputSpeech = {
  type: 'SSML',
  ssml: ssml
};
return response;