Search code examples
aws-lambdaalexa-skills-kit

How to close an Alexa skill that uses AudioPlayer interface


I am currently developing an Alexa skill where I use the AudioPlayer interface to play a song.

When the Cancel intent is called I do a :

'AMAZON.CancelIntent': function() {
this.emit(':tell', "Bye!");}

The problem is that after alexa said "Bye", the audio is unpaused and the skill is still running.

How can I close the skill from an Intent handler?


Solution

  • You have to send an AudioPlayer.Stop directive to stop any currently playing audio stream.

    'AMAZON.CancelIntent': function() {
       this.response.speak('Bye').audioPlayerStop();
       this.emit(':responseReady');
    }
    

    In ask-sdk-v2 you can use:

    return handlerInput.responseBuilder
          .addAudioPlayerStopDirective()
          .getResponse();
    

    Update
    Alexa service will track your audio player skill as the skill that most recently streamed audio and for any built in intent, Alexa will create a new session and trigger the request to your skill.

    When your skill sends a Play directive to begin playback, the Alexa service sends the audio stream to the device for playback. Once the session ends normally (for instance, if your response included the shouldEndSession flag set to true), Alexa remembers that your skill started the playback until the user does one of the following:

    1. Invokes audio playback with a different skill.
    2. Invokes another service that streams audio, such as the built-in music service or the flash briefing.
    3. Reboots the device.

    Check this documentation Built-in Intents for Playback Control

    More on AudioPlayer directives here