【问题标题】:Alexa Audio Player DirectiveAlexa 音频播放器指令
【发布时间】:2018-06-11 20:02:41
【问题描述】:

我正在尝试构建一个可以播放音频文件的 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
                }
            }
        })
    }
};

【问题讨论】:

    标签: node.js alexa alexa-skills-kit alexa-skill alexa-voice-service


    【解决方案1】:

    您必须在处理程序中返回“构建”响应。所以在你的情况下,代码是:

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

    【讨论】:

      【解决方案2】:

      如果您使用的是 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 中用于播放音频的代码 sn-p。

      //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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-06
        • 2017-12-01
        • 1970-01-01
        • 2022-12-21
        • 2019-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多