question

Viergelenker avatar image
Viergelenker asked

Alexa java sdk: How to enqueue songs onPlaybackNearlyFinished? (AudioPlayer)

I'm developing a skill which streams audio files. Playback is working fine, even pause, resume and "getNextSong" is working, but i can't enqueue a song after the first one finishes.

@Override
public SpeechletResponse onPlaybackNearlyFinished(SpeechletRequestEnvelope<PlaybackNearlyFinishedRequest> requestEnvelope) {
    logger.info("Playback nearly finished");
    return queueNextSong(requestEnvelope);
}

Enqueue method:

private SpeechletResponse queueNextSong(SpeechletRequestEnvelope<PlaybackNearlyFinishedRequest> requestEnvelope) {

    SystemState state = requestEnvelope.getContext().getState(SystemInterface.class, SystemState.class);

    logger.info("Queue next song");

    UserSession userSession = null;

    try {
        userSession = new DynamoDbService().getUserSession(state.getUser().getUserId());
    } catch (Exception e) {
        logger.error("Unable to obtain userSession Object.");
    }

    logger.info("User session obtained");

    String url = null;
    // Don't worry about this line, i just use the last object of my songList because it's cheaper to remove than the first entry.
    url = userSession.getSongQueue().get(userSession.getSongQueue().size()-1);

    Stream stream = new Stream();
    stream.setUrl(url);
    stream.setOffsetInMilliseconds(0);
    stream.setExpectedPreviousToken(requestEnvelope.getRequest().getToken());
    stream.setToken(url);

    AudioItem audioItem = new AudioItem();
    audioItem.setStream(stream);

    PlayDirective playDirective = new PlayDirective();
    playDirective.setAudioItem(audioItem);
    playDirective.setPlayBehavior(PlayBehavior.ENQUEUE);

    List<Directive> directives = new ArrayList<>();
    directives.add(playDirective);

    SpeechletResponse response = new SpeechletResponse();
    response.setDirectives(directives);

    logger.info("Return enqueue response");
    return response;
}

What am i missing? I need some help please... i have read the official documentation and checked the forum here but none of the solutions are working. :(

alexa skills kithow-toaudioresponseaudioplayer
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Jenn@amazon avatar image
Jenn@amazon answered

Unfortunatley I'm not fluent in Java, but I'm pretty good with AudioPlayer.

Are you able to output the Response JSON you're sending back to Alexa? If you're using Lambda, take the Request Event for PlaybackNearlyFinished and stick it into a Test event. The output of the Test event will show up in the Lambda Dashboard.

1 comment
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Viergelenker avatar image Viergelenker commented ·

Thank you so much for your reply! Absolutely no idea why, but my skill works now?? I haven't touched my code since i posted the question because i was so frustrated >:( I just put my enqueue method in a custom intent in order to test it for you, and was like yeah i will just test it one more time on my echo dot just to make sure and now it works. wtf! Sorry for wasting your time, but i was so frustrated because i had a feeling i'm doing the right thing and i already spend too many hours with this. Below is the response json minus the prev. token which isn't included in a regular intent (i just used an intent to test it for you, usually i use the playbackNearlyFinished event). My skill is now working the expected way and automatically enqueues all songs i store in my db.

Thanks again for your reply!

{ 
  "version": "1.0",
  "response": {
    "shouldEndSession": true,
    "directives": [
      {
        "type": "AudioPlayer.Play",
        "playBehavior": "ENQUEUE",
        "audioItem": {
          "stream": {
            "token": "https://s3-eu-west-1.amazonaws.com/bucket-vollweiter/05_-_Kinder_wollen_singen_-_Backe_backe_Kuchen.mp3",
            "url": "https://s3-eu-west-1.amazonaws.com/bucket-vollweiter/05_-_Kinder_wollen_singen_-_Backe_backe_Kuchen.mp3",
            "offsetInMilliseconds": 0
          }
        }
      }
    ]
  }
}
1 Like 1 ·