使用 Alexa 流式传输音频的最简单示例
Simplest example for streaming audio with Alexa
我正在尝试启动新的流式音频 API。以下回复有效吗?我在我的设备上测试时遇到 "there was a problem with the skill" 错误。
这是我的 AWS-lambda 函数的代码:
def lambda_handler(event, context):
return {
"response": {
"directives": [
{
"type": "AudioPlayer.Play",
"playBehavior": "REPLACE_ALL",
"audioItem": {
"stream": {
"token": "12345",
"url": "http://emit-media-production.s3.amazonaws.com/pbs/the-afterglow/2016/08/24/1700/201608241700_the-afterglow_64.m4a",
"offsetInMilliseconds": 0
}
}
}
],
"shouldEndSession": True
}
}
以下代码对我有用:
def lambda_handler(event, context):
return {
"response": {
"directives": [
{
"type": "AudioPlayer.Play",
"playBehavior": "REPLACE_ALL",
"audioItem": {
"stream": {
"token": "12345",
"url": "https://emit-media-production.s3.amazonaws.com/pbs/the-afterglow/2016/08/24/1700/201608241700_the-afterglow_64.m4a",
"offsetInMilliseconds": 0
}
}
}
],
"shouldEndSession": True
}
}
]
唯一的区别是 URL 是 https 而不是 http。
如果它在技能模拟器中不起作用,请不要推迟。尚未升级以使用流式音频。你甚至不会在那里看到你的指令。但它应该与您的设备一起使用。
一个程序应该 return 在 "LaunchRequest" 和 "SessionEndedRequest" 上有一些响应,否则你会得到 "There was a problem with the requested skills repsonse".
您需要添加意图 "PlayMusic" 并更改文件 url。
P.S。我不确定哪个 version
应该在 build_audio_response
函数中,我从 here
得到了 json
def build_audio_response(url):
return {
"version": "1.01",
"response": {
"directives": [
{
"type": "AudioPlayer.Play",
"playBehavior": "REPLACE_ALL",
"audioItem": {
"stream": {
"token": "12345",
"url": url,
"offsetInMilliseconds": 0
}
}
}
],
"shouldEndSession": True
}
}
def handle_session_end_request():
return {
"version": "1.0",
"response": {
"shouldEndSession": True
}
}
def play_music(intent, session):
url = "https://s3-eu-west-1.amazonaws.com/bucket/filename.mp3"
return build_audio_response(url, should_end_session=True)
def on_intent(intent_request, session):
""" Called when the user specifies an intent for this skill """
intent = intent_request['intent']
intent_name = intent_request['intent']['name']
if intent_name == "PlayMusic":
return play_music(intent, session)
elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent":
return handle_session_end_request()
else:
raise ValueError("Invalid intent")
def lambda_handler(event, context):
if event['request']['type'] == "LaunchRequest":
return {
"version": "1.0",
"response": {
"shouldEndSession": False
}
}
elif event['request']['type'] == "IntentRequest":
return on_intent(event['request'], event['session'])
elif event['request']['type'] == "SessionEndedRequest":
return handle_session_end_request()
我们在 Github 上创建了一个非常简单的项目,展示了使用 AudioPlayer 的最简单方法:
https://github.com/bespoken/super-simple-audio-player
我们也在这里为它写了一篇文章:
https://bespoken.tools/blog/2017/02/27/super-simple-audioplayer
该项目展示了如何播放曲目,以及如何暂停和恢复。
下面是显示音频文件实际播放的代码:
SimplePlayer.prototype.play = function (audioURL, offsetInMilliseconds) {
var response = {
version: "1.0",
response: {
shouldEndSession: true,
directives: [{
type: "AudioPlayer.Play",
playBehavior: "REPLACE_ALL", // Setting to REPLACE_ALL means that this track will start playing immediately
audioItem: {
stream: {
url: audioURL,
token: "0", // Unique token for the track - needed when queueing multiple tracks
expectedPreviousToken: null, // The expected previous token - when using queues, ensures safety
offsetInMilliseconds: offsetInMilliseconds
}
}
}]
}
}
this.context.succeed(response);
};
我正在尝试启动新的流式音频 API。以下回复有效吗?我在我的设备上测试时遇到 "there was a problem with the skill" 错误。
这是我的 AWS-lambda 函数的代码:
def lambda_handler(event, context):
return {
"response": {
"directives": [
{
"type": "AudioPlayer.Play",
"playBehavior": "REPLACE_ALL",
"audioItem": {
"stream": {
"token": "12345",
"url": "http://emit-media-production.s3.amazonaws.com/pbs/the-afterglow/2016/08/24/1700/201608241700_the-afterglow_64.m4a",
"offsetInMilliseconds": 0
}
}
}
],
"shouldEndSession": True
}
}
以下代码对我有用:
def lambda_handler(event, context):
return {
"response": {
"directives": [
{
"type": "AudioPlayer.Play",
"playBehavior": "REPLACE_ALL",
"audioItem": {
"stream": {
"token": "12345",
"url": "https://emit-media-production.s3.amazonaws.com/pbs/the-afterglow/2016/08/24/1700/201608241700_the-afterglow_64.m4a",
"offsetInMilliseconds": 0
}
}
}
],
"shouldEndSession": True
}
}
]
唯一的区别是 URL 是 https 而不是 http。
如果它在技能模拟器中不起作用,请不要推迟。尚未升级以使用流式音频。你甚至不会在那里看到你的指令。但它应该与您的设备一起使用。
一个程序应该 return 在 "LaunchRequest" 和 "SessionEndedRequest" 上有一些响应,否则你会得到 "There was a problem with the requested skills repsonse".
您需要添加意图 "PlayMusic" 并更改文件 url。
P.S。我不确定哪个 version
应该在 build_audio_response
函数中,我从 here
def build_audio_response(url):
return {
"version": "1.01",
"response": {
"directives": [
{
"type": "AudioPlayer.Play",
"playBehavior": "REPLACE_ALL",
"audioItem": {
"stream": {
"token": "12345",
"url": url,
"offsetInMilliseconds": 0
}
}
}
],
"shouldEndSession": True
}
}
def handle_session_end_request():
return {
"version": "1.0",
"response": {
"shouldEndSession": True
}
}
def play_music(intent, session):
url = "https://s3-eu-west-1.amazonaws.com/bucket/filename.mp3"
return build_audio_response(url, should_end_session=True)
def on_intent(intent_request, session):
""" Called when the user specifies an intent for this skill """
intent = intent_request['intent']
intent_name = intent_request['intent']['name']
if intent_name == "PlayMusic":
return play_music(intent, session)
elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent":
return handle_session_end_request()
else:
raise ValueError("Invalid intent")
def lambda_handler(event, context):
if event['request']['type'] == "LaunchRequest":
return {
"version": "1.0",
"response": {
"shouldEndSession": False
}
}
elif event['request']['type'] == "IntentRequest":
return on_intent(event['request'], event['session'])
elif event['request']['type'] == "SessionEndedRequest":
return handle_session_end_request()
我们在 Github 上创建了一个非常简单的项目,展示了使用 AudioPlayer 的最简单方法:
https://github.com/bespoken/super-simple-audio-player
我们也在这里为它写了一篇文章:
https://bespoken.tools/blog/2017/02/27/super-simple-audioplayer
该项目展示了如何播放曲目,以及如何暂停和恢复。
下面是显示音频文件实际播放的代码:
SimplePlayer.prototype.play = function (audioURL, offsetInMilliseconds) {
var response = {
version: "1.0",
response: {
shouldEndSession: true,
directives: [{
type: "AudioPlayer.Play",
playBehavior: "REPLACE_ALL", // Setting to REPLACE_ALL means that this track will start playing immediately
audioItem: {
stream: {
url: audioURL,
token: "0", // Unique token for the track - needed when queueing multiple tracks
expectedPreviousToken: null, // The expected previous token - when using queues, ensures safety
offsetInMilliseconds: offsetInMilliseconds
}
}
}]
}
}
this.context.succeed(response);
};