maxymoo
maxymoo

Reputation: 36545

Simplest example for streaming audio with Alexa

I'm trying to get the new streaming audio API going. Is the following response valid? I'm getting a "there was a problem with the skill" error when I test it on my device.

Here is the code for my AWS-lambda function:

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

Upvotes: 14

Views: 10788

Answers (3)

John Kelvie
John Kelvie

Reputation: 858

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

Upvotes: 4

armicron
armicron

Reputation: 309

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

Upvotes: 0

Joseph Jaquinta
Joseph Jaquinta

Reputation: 2138

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.

Upvotes: 19

Related Questions