How to make Alexa countdown in seconds

馋奶兔 提交于 2021-01-27 11:52:12

问题


I want to be able to have alexa (audibly) countdown 15 seconds in my skill. I know I can just <break time="15s" /> in SSML. But that isn't audible. I also know I can just do:

15<break time="1s" />
14<break time="1s" /> 

or better yet (to account for the time it takes to say the number)

15<break time="0.85s" />
14<break time="0.85s" />

But that's going to be a ton of repeated code if I do this many times over. So I'm probably going to write a function that takes in a number and a number of seconds, and produces an SSML countdown in that interval.

Before I do that, however, I was wondering if there's a proper, built-in way of doing this? Or if someone has a function they've already built for this? Thanks!!!


回答1:


function buildCountdown(seconds, break) {
    var countdown = "";

    for (var i = seconds; i > 0; i--) {
        var count = i.toString + "<break time='" + break.toString() + "s' />\n";
        countdown.concat(count);
    }

    return countdown;
}

And then just provide the outputSpeech property:

"outputSpeech": {
    "type": "SSML",
    "ssml": buildCountdown(15, 0.85)
}

I'm not sure about any ASK built-ins for building SSML, but writing functions that generate markup is pretty common when working with Javascript frameworks, so it seems appropriate here.




回答2:


I ended up with the following function (with the help of someone on the Alexa slack):

function countDown(numSeconds, breakTime) {
    return Array.apply(null, {length: numSeconds})
        .map((n, i) => {return `<say-as interpret-as="cardinal">${numSeconds-i}</say-as>` })
        .join(`<break time="${breakTime ? breakTime : 0.85}s" />`) + `<break time="${breakTime ? breakTime : 0.85}s" />`;
}


来源:https://stackoverflow.com/questions/51388933/how-to-make-alexa-countdown-in-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!