How to add multiple tracks to a stream in alexa?

I am working on alexa for the first time, and I am developing a music application. I need to add several tracks of one artist and play it continuously. I can not do it. However, one song works fine, but cannot add and play multiple songs.

Here is my code

$response = '{ "version" : "1.0", "response" : { "outputSpeech": { "type": "PlainText", "text": "Playing song for Acon" }, '.$card.', "directives": [ { "type": "AudioPlayer.Play", "playBehavior": "REPLACE_ALL", "audioItem": { "stream": { "token": "track1", "url": "https://p.scdn.co/mp3-preview/9153bcc4d7bef50eb80a809fa34e694f2854e539?cid=null", "offsetInMilliseconds": 0 } } } ], "shouldEndSession" : true } }'; 
+5
source share
2 answers

Follow the above document: https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/custom-audioplayer-interface-reference#playbacknearlyfinished-request

and then do it to insert a second song

In PHP, how you handle other requests, you can also handle AudioRequest. For instance.

 $data = file_get_contents("php://input"); $jsonData = json_decode($data); if($jsonData->request->type === "AudioPlayer.PlaybackNearlyFinished") { $response = '{ "version" : "1.0", "response" : { "directives": [ { "type": "AudioPlayer.Play", "playBehavior": "ENQUEUE", "audioItem": { "stream": { "token": "track2", "expectedPreviousToken": "track1", "url": "Your URL", "offsetInMilliseconds": 3 } } } ], "shouldEndSession" : true } }'; echo $response; } 

This way you can handle all audio requests.

+1
source

You need to wait for the request AudioPlayer.PlaybackNearlyFinished from Alexa. At this point, you can queue the next track for playback. It will be close to ending playback of the currently playing track.

Information about him is here:
https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/custom-audioplayer-interface-reference#playbacknearlyfinished-request

When responding with the PlaybackNearlyFinished directive, make sure that:

  • You set playBehavior to ENQUEUE
    This will cause the next track to begin after the current one ends.

  • You DO NOT include the outputSpeech field
    outputSpeech is not allowed if it does not work. A session ends when the first stream starts playback.

This blog post I wrote describes in more detail the approaches to developing and testing the AudioPlayer interface:
https://bespoken.tools/blog/2016/10/10/unit-testing-alexa-skills

+1
source

Source: https://habr.com/ru/post/1262677/


All Articles