Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
        }
    }
like image 819
maxymoo Avatar asked Aug 26 '16 02:08

maxymoo


People also ask

How do I stream audio to Alexa?

Say “Alexa, pair Bluetooth,” and your Echo will go into pairing mode. You can then connect any mobile device or computer to it over Bluetooth, and stream audio across the connection.

Can Alexa stream audio?

Stream music, videos, podcasts, audiobooks, and more and control it with Alexa. It's easy to pair a Bluetooth device with an Amazon Alexa speaker. Once paired, it's fast to pair it again with a simple voice command to stream audio via Bluetooth.

How do I stream YouTube audio to Alexa?

Connect Your Devices to Play YouTube Audio on Echo To do this easily, you can say "Alexa, connect Bluetooth." This will cause your Echo to connect to your phone, since it's a previously paired device. Alexa will make a sound and say "Now connected to [Device Name]" if it's successful.


2 Answers

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.

like image 53
Joseph Jaquinta Avatar answered Oct 21 '22 07:10

Joseph Jaquinta


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);
};
like image 23
John Kelvie Avatar answered Oct 21 '22 06:10

John Kelvie