jovotech / jovo-templates

💬 Code Patterns and Templates for Jovo Voice Apps
https://www.jovo.tech
Apache License 2.0
27 stars 30 forks source link

AudioPlayer.PlaybackNearlyFinished triggers too early #19

Closed kevinApplaud closed 6 years ago

kevinApplaud commented 6 years ago

I need to play a list of mp3 urls.

Right now, when using your template then: PlaybackNearlyFinished triggers too early, I need to play a list of mp3 files. But It only plays them for 1 sec and then plays next one for 1 sec etc...

Code

"use strict";

// =================================================================================
// App Configuration
// =================================================================================

const { App } = require("jovo-framework");

const config = {
  logging: true,
  intentMap: {
    "AMAZON.PauseIntent": "PauseIntent",
    "AMAZON.ResumeIntent": "ResumeIntent"
  }
};

const app = new App(config);
let mp3Arr;

let currentMp3Count = 0;
app.setHandler({
  LAUNCH: function() {
    this.toIntent("PlayIntent");
  },

  PlayIntent: function() {
    mp3Arr = [
      "https://s3.amazonaws.com/jovo-songs/song1.mp3",
      "https://s3.amazonaws.com/jovo-songs/song1.mp3"
    ];
    this.alexaSkill()
      .audioPlayer()
      .setOffsetInMilliseconds(0)
      .play(mp3Arr[currentMp3Count], "token")
      .tell("Playing news");
  },

  PauseIntent: function() {
    this.alexaSkill()
      .audioPlayer()
      .stop();
    this.tell("Paused!");
  },

  ResumeIntent: function() {
    this.alexaSkill()
      .audioPlayer()
      .setOffsetInMilliseconds(0)
      .play(news[currentNews].url, "token")
      .tell("Resuming!");
  },

  StopIntent: function() {
    console.log("StopIntent");
    this.endSession();
  },

  AUDIOPLAYER: {
    "AudioPlayer.PlaybackStarted": function() {
      console.log("AudioPlayer.PlaybackStarted");
      this.endSession();
    },

    "AudioPlayer.PlaybackNearlyFinished": function() {
      console.log("AudioPlayer.PlaybackNearlyFinished");
      currentMp3Count++;

      if (currentMp3Count < mp3Arr.length) {
        this.alexaSkill()
          .audioPlayer()
          .setOffsetInMilliseconds(0)
          .play(mp3Arr[currentMp3Count], "token");
      } else {
        currentMp3Count = 0;
        this.toIntent("StopIntent");
      }
    },

    "AudioPlayer.PlaybackFinished": function() {
      console.log("AudioPlayer.PlaybackFinished");
      this.endSession();
    },

    "AudioPlayer.PlaybackStopped": function() {
      console.log("AudioPlayer.PlaybackStopped");
      this.endSession();
    }
  }
});

module.exports.app = app;
jankoenig commented 6 years ago

Answered here https://github.com/jovotech/jovo-framework-nodejs/issues/244