martylietz02
martylietz02

Reputation: 21

How to loop audio in Alexa

I am building a ambient audio skill for sleep for Alexa! I am trying to loop the audio so I don't have to download 10 hour versions of the audio. How do I get the audio to work? I have it build to where it will play the audio, but not loop.

Upvotes: 2

Views: 1547

Answers (1)

Arif Gebhardt
Arif Gebhardt

Reputation: 111

I've solved this problem in my skill Rainmaker: https://www.amazon.com/Arif-Gebhardt-Rainmaker/dp/B079V11ZDM

The trick is to handle the PlaybackNearlyFinished event. https://developer.amazon.com/de/docs/alexa-voice-service/audioplayer.html#playbacknearlyfinished

This event is fired shortly before the currently playing audio stream is ending. Respond to the event with another audioPlayerPlay directive with behavior ENQUEUE. This will infinitely loop your audio until it gets interrupted by e.g. the AMAZON.StopIntent.

Advanced: if you want a finite loop, say ten times your audio, use the token of the audioPlayerPlay directive to count down from ten. Once the counter hits zero, just don't enqueue another audio. But be sure to respond something in this case, even if it's just an empty response. Otherwise you will get a timeout error or the like.

Upvotes: 4

Related Questions