Simplest example for streaming audio with Alexa

妖精的绣舞 提交于 2019-11-30 12:13:32
Joseph Jaquinta

The following code worked for me:

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

The only difference is that the URL is https rather than http.

Don't be put off if it doesn't work in the skill simulator. That hasn't been upgraded yet to work with streaming audio. You won't even see your directives there. But it should work when used with your device.

We created a really simple project on Github that shows the easiest possible way to use the AudioPlayer:
https://github.com/bespoken/super-simple-audio-player

We also created a writeup for it here:
https://bespoken.tools/blog/2017/02/27/super-simple-audioplayer

The project shows how to play a track, as well pausing and resuming.

Here is the code that shows the actual playing of an audio file:

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

A program should return some response on "LaunchRequest" and "SessionEndedRequest" otherwise you will get "There was a problem with the requested skills repsonse".

You need to add intent "PlayMusic" and change url of the file.

P.S. I am not sure which version should be in build_audio_response function, I got the json from 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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!