Bespoken LLM Benchmark: Does ChatGPT know more than Google and Amazon?
Learn more
February 27, 2017 in Blog

The Super Simple AudioPlayer

TL;DR How to use the Alexa AudioPlayer in the easiest way possible—no code dependencies, no frameworks, structured as JavaScript/NodeJS Lambda. Get started.
audio player

We’ve created a new Github project, the Super Simple AudioPlayer, showing how to use the Alexa AudioPlayer in the easiest possible way.

The project has no code dependencies, uses no frameworks, and is meant to illustrate as simply and transparently as possible how the AudioPlayer works. We hope you find it useful!

It is structured as JavaScript/NodeJS Lambda. To get started using it, just follow our READ ME.

What does this skill do?

  • It plays an introduction to the user explaining how it works
  • It plays a hard-coded MP3 audio file when the user says “Play”
  • It can Pause and Resume, using a simple in-memory cache

How can I use it?

Short answer – any way that you want 😉

Really, though, this is meant as a starting point for people wanting to understand how the AudioPlayer works and do some basic things with it.

For that reason, it is heavily stripped down so that people can see the behavior up-close, without frameworks getting in the way. For example, here is the code for playing a track:

/**
* Plays a particular track, from specific offset
* @param audioURL The URL to play
* @param offsetInMilliseconds The point from which to play - we set this to something other than zero when resuming
*/
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);
};
view raw index.js hosted with ❤ by GitHub

As you can see, the distance between the code and the raw Alexa JSON is absolutely minimal.

What is Next with the Alexa AudioPlayer?

We plan on expanding on this project, as a series of branches on the repository that evolve this simple example into a fully functional player. With each addition, it will still be possible to easily access the previous, simpler version. We hope this incremental approach keeps the project simultaneously easy to understand while also guiding readers to a full-fledged implementation.

Features we plan to add:

  • Use of Playlists
  • Saving state with DynamoDB
  • Unit and Functional Testing
  • Shuffle and loop

Let us know if there are other aspects you would like to see covered – we welcome the input! Just comment below. And watch or follow the project on GitHub.

5 responses to “The Super Simple AudioPlayer”

  1. Hey John – I wrote a few months ago about my experimentation with getting KPIG radio to stream to both my Echo Show as well as original Echo. (For some reason the Echo Show was able to handle a redirect, whereas the Echo and 1st gen Dot were not.) Well, Amazon must’ve changed things in AudioPlayer because when I tried it again tonight, it has no problems with the redirect. Woot!

    Now, I just wish they would support Shoutcast Metadata Protocol so that it would show me what’s playing on the Echo Show and I could just ask on the Echo and the Dot. :-}

    I suppose I could code something to make the request. Aiyeee, I’m gonna have to learn how to code! :-}

    • Oh that is pretty neat with the redirects. I did not think that they worked, so that is certainly useful that they do now.

      Shoutcast Metadata is embedded in the stream correct? Some services do provide external servers you can call to access it as well – that might work well. Good luck with it!

  2. Sidenote – man, don’t know how I’d be able debug this without bst proxy. I finally moved my functions to Lambda after not being able to autostart node on the C.H.I.P. I have at home (also running two instances on two separate ports was a problem.) Setup a second skill and totally missed that I hadn’t enabled AudioPlayer, the error for which didn’t show up in CloudWatch or even the Session tester. It was only when I ran through bst proxy that I saw the problem. How do people debug this kind of stuff without bst proxy? Crazy? Thank you!!

Leave a Reply

Your email address will not be published. Required fields are marked *