У меня есть один аудио навык в Алексе. Но есть одна проблема: проигрывать аудио всего несколько секунд, а затем останавливать.
мой код:
const Alexa = require('ask-sdk-core');
let currentAudioNum = 0;
let skill;
const STREAMS = [
{
.........
},
{
.........
},
];
const PlayStreamIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest' ||
(handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'PlayStreamIntent') ||
(handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.ResumeIntent');
},
handle(handlerInput) {
let stream = STREAMS[currentAudioNum];
handlerInput.responseBuilder
.speak(`starting swaminarayan kirtan bhakti`)
.addAudioPlayerPlayDirective('REPLACE_ALL', stream.url, stream.token, 0, null, stream.metadata);
return handlerInput.responseBuilder
.getResponse();
},
};
const PlayNextStreamIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.NextIntent';
},
handle(handlerInput) {
currentAudioNum = currentAudioNum + 1;
let stream = STREAMS[currentAudioNum];
handlerInput.responseBuilder
.addAudioPlayerPlayDirective('REPLACE_ALL', stream.url, stream.token, 0, null, stream.metadata);
return handlerInput.responseBuilder
.getResponse();
},
};
const PlayPrevStreamIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.PreviousIntent';
},
handle(handlerInput) {
currentAudioNum = currentAudioNum - 1;
let stream = STREAMS[currentAudioNum];
handlerInput.responseBuilder
.addAudioPlayerPlayDirective('REPLACE_ALL', stream.url, stream.token, 0, null, stream.metadata);
return handlerInput.responseBuilder
.getResponse();
},
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speechText = 'This skill just plays an audio stream when it is started. It does not have any additional functionality.';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
},
};
const AboutIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AboutIntent';
},
handle(handlerInput) {
const speechText = 'This is an audio starter template from skill templates dot com';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
},
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.PauseIntent');
},
handle(handlerInput) {
handlerInput.responseBuilder
.addAudioPlayerClearQueueDirective('CLEAR_ALL')
.addAudioPlayerStopDirective();
return handlerInput.responseBuilder
.getResponse();
},
};
const PlaybackStoppedIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'AudioPlayer.PlaybackStopped';
},
handle(handlerInput) {
//should save details so play can be resumed.
return true;
},
};
//AudioPlayer.PlaybackStarted
const PlaybackStartedIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'AudioPlayer.PlaybackStarted';
},
handle(handlerInput) {
handlerInput.responseBuilder
.addAudioPlayerClearQueueDirective('CLEAR_ENQUEUED');
return handlerInput.responseBuilder
.getResponse();
},
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.PauseIntent');
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
return handlerInput.responseBuilder
.getResponse();
},
};
//System.ExceptionEncountered
const ExceptionEncounteredRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'System.ExceptionEncountered';
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
return true;
},
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
return handlerInput.responseBuilder
.addAudioPlayerClearQueueDirective('CLEAR_ALL')
.addAudioPlayerStopDirective()
.getResponse();
},
};
exports.handler = async function (event, context) {
console.log(`REQUEST++++${JSON.stringify(event)}`);
if (!skill) {
skill = Alexa.SkillBuilders.custom()
.addRequestHandlers(
PlayStreamIntentHandler,
PlayNextStreamIntentHandler,
PlayPrevStreamIntentHandler,
PlaybackStartedIntentHandler,
CancelAndStopIntentHandler,
PlaybackStoppedIntentHandler,
AboutIntentHandler,
HelpIntentHandler,
ExceptionEncounteredRequestHandler,
SessionEndedRequestHandler
)
.addErrorHandlers(ErrorHandler)
.create();
}
return skill.invoke(event,context);
}