【问题标题】:How can I make my Alexa skill play a simple mp3 file?如何让我的 Alexa 技能播放简单的 mp3 文件?
【发布时间】:2020-03-12 06:20:14
【问题描述】:

我正在尝试编写一个仅播放 MP3 文件的 Alexa 技能。我用以下代码修改了默认的“hello world”lambda:

const Alexa = require('ask-sdk-core');

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle(handlerInput) {
        return {
        "response": {
            "directives": [
                {
                    "type": "AudioPlayer.Play",
                    "playBehavior": "REPLACE_ALL",
                    "audioItem": {
                        "stream": {
                            "token": "12345",
                            "url": "https://jpc.io/r/brown_noise.mp3",
                            "offsetInMilliseconds": 0
                        }
                    }
                }
            ],
            "shouldEndSession": true
        }
        }
    }
};
exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        LaunchRequestHandler,
    )
    .lambda();

但是当我部署代码并调用技能时,它不会发出任何声音或报告任何错误。当我用

替换 handle 代码时
const speakOutput = 'Hello world';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();

然后她在被调用时打招呼。

我的问题:为什么她会和我说话,但似乎不播放我的 mp3?我见过其他关于堆栈溢出的问题,原因是他们没有使用 https,但我使用的是 https。

【问题讨论】:

  • 你如何测试这个?开发控制台不支持音频接口,因此测试您的技能的唯一方法是使用物理设备,或者手机应用程序也应该可以工作。

标签: alexa alexa-skills-kit alexa-skill alexa-sdk-nodejs ask-sdk


【解决方案1】:

在撰写本文时,有一种直接发出 AudioPlayer 指令的新方法。

const PlayStreamIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'LaunchRequest'
      || handlerInput.requestEnvelope.request.type === 'IntentRequest';
  },
  handle(handlerInput) {
    const stream = STREAMS[0];

    handlerInput.responseBuilder
      .speak(`starting ${stream.metadata.title}`)
      .addAudioPlayerPlayDirective('REPLACE_ALL', stream.url, stream.token, 0, null, stream.metadata);

    return handlerInput.responseBuilder
      .getResponse();
  },
};

关键函数是.addAudioPlayerPlayDirective(); 您可以在Alexa Skills Kit API 上阅读有关这些指令函数的更多信息,尽管那里的一些代码似乎已经过时。 Building Response 页面列出了较新的音频播放器功能。

STREAMS 数组的示例:

const STREAMS = [
  {
    token: '1',
    url: 'https://streaming.radionomy.com/-ibizaglobalradio-?lang=en-US&appName=iTunes.m3u',
    metadata: {
      title: 'Stream One',
      subtitle: 'A subtitle for stream one',
      art: {
        sources: [
          {
            contentDescription: 'example image',
            url: 'https://s3.amazonaws.com/cdn.dabblelab.com/img/audiostream-starter-512x512.png',
            widthPixels: 512,
            heightPixels: 512,
          },
        ],
      },
      backgroundImage: {
        sources: [
          {
            contentDescription: 'example image',
            url: 'https://s3.amazonaws.com/cdn.dabblelab.com/img/wayfarer-on-beach-1200x800.png',
            widthPixels: 1200,
            heightPixels: 800,
          },
        ],
      },
    },
  },
];

代码 sn-ps 取自 this example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-09
    • 2014-12-07
    • 2020-03-28
    • 2013-05-18
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    相关资源
    最近更新 更多