javascriptaudioalexa-skills-kitamazon-echo

Amazon Echo Playing Music on Command


Hey everyone so I'm having a difficult time trying to implement an audio playback. Here's the docs

So what I really want to do seems quite simple but came out to be very confusing where everything is suppose to go.

I want to be able to say a command. Alexa will respond will a little outputSpeech and then proceed to play a small audio track mp3 that I would provide. I don't mind uploading it locally(When I zip the files and import them into a lamda function) or using S3 Buckets SDK to stream the mp3 file. Which ever is easier for you guys.

Here's what I got so far.

With the codes below I'm able to get Alexa to respond to be voice and output a speech.

I'm only using the IntentRequest to reduce codes for you guys.

exports.handler = (event, context, callback) => {
    try {
        if (event.request.type === 'IntentRequest') {
            onIntent(event.request,
                event.session,
                (sessionAttributes, speechletResponse) => {
                    callback(null, buildResponse(sessionAttributes, speechletResponse));
                });
        }
    } catch (err) {
        callback(err);
    }
};


My function that will be called when the Intent request goes through


function onIntent(intentRequest, session, callback) {
    console.log(`onIntent requestId=${intentRequest.requestId}, sessionId=${session.sessionId}`);

    const intent = intentRequest.intent;
    const intentName = intentRequest.intent.name;

    if (intentName === 'PlayMyMusic') {
        PlayMyMusic(intent, session, callback);
    } else if (intentName === 'AMAZON.StopIntent' || intentName === 'AMAZON.CancelIntent') {
        handleSessionEndRequest(callback);
    } else {
        throw new Error('Invalid intent');
    }
}


This is the output Message

function PlayMyMusic(intent, session, callback) {

    const repromptText = null;
    const sessionAttributes = {};
    let shouldEndSession = true;
    let speechOutput = '';

        speechOutput = `I'm Alexa and I will output speech in this area. After I'm done talking I will play an audio track`;

    callback(sessionAttributes,
        buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession));
}

This is my simple Intent Schema

{
  "intents": [
    {
      "intent": "PlayMyMusic"
    },
    {
      "intent": "AMAZON.HelpIntent"
   }
  ]
}

Sample Utterances

PlayMyMusic play my music


Everything works as of right now where Amazon can talk to me back and end the session.

How would I be able to have Amazon responds to me and then play some audio? The docs are kind of not working for me.

  • Where do I put the play directive? (AudioPlayer.Play)

  • Solution

  • you can use SSML and add mp3 path at any https url and it will play song

    see this

    Include the audio tag within your text-to-speech response within the speak tag. Alexa plays the MP3 at the specified point within the text to speech. For example:
    <speak>
        Welcome to Car-Fu. 
        <audio src="soundbank://soundlibrary/transportation/amzn_sfx_car_accelerate_01" /> 
        You can order a ride, or request a fare estimate. 
        Which will it be?
    </speak> 
    When Alexa renders this response, it would sound like this:
    Alexa: Welcome to Car-Fu.
    (the specified amzn_sfx_car_accelerate_01.mp3 audio file plays)
    Alexa: You can order a ride, or request a fare estimate. Which will