Search code examples
pythonalexa-skills-kit

Is it possible to play audio with the ASK AudioPlayer Interface, and then return to session?


I'm building an alexa skill, and at certain points in the skill, I would like to play short 30 second audio clips. I'm already aware that this can easily be done using the SSML <audio> tag, however I really like the features that the AudioPlayer interface has including the ability to play audio at a particular offset, see if the audio is nearly finished, be able to pause the audio, etc.

I'm wondering if there is a way to use the AudioPlayer to play the audio clip, and then continue the skill session and be able to resume to exactly where you left off before.

For example, in an intent handler like this:

def play_audio_handler(handler_input):

    spoken_text = "Audio will now play. "
    reprompt_text = spoken_text
    
    handler_input.response_builder.speak(spoken_text).ask(reprompt_text).add_directive(
        PlayDirective(
            play_behavior=PlayBehavior.REPLACE_ALL,
            audio_item=AudioItem(
                stream=Stream(
                    token="token",
                    url="{some audio url}",
                    offset_in_milliseconds=0,
                    expected_previous_token=None),
                metadata=None))
    ).set_should_end_session(False)
    
    return handler_input.response_builder.response

I would want to be able to then make more intent requests after this to be able to continue using the alexa skill

Can this be done?

I have tried changing the value of .set_should_end_session(), but it seems like the session has to end before the audio can play for some reason.


Solution

  • Nope, you can't resume the session.

    Best practice for this is to send your audio response back in an APLA document.

    APL and APLA give you great control over playback. For example, to start or end playback at specific points, use a Trim filter in your APLA document.

    {
         "type": "Audio",
         "source": "https://example.com/audio_track.mp3",
         "filter": [
             {
                 "type": "Trim",
                 "start": 2000,
                 "end": 9000
             }
         ]
    }
    

    APLA doesn't automatically pause but you can work around that with intent / event handlers and APL controls.

    It also won't automatically handle "nearly finished", but you should be able to do that yourself by combining the Sequencer and Mix objects and the SendEvent command.