I have created a skill for Amazon Alexa using node.js, which plays an MP3 stream. Now I have problems to play a jingle with a fixed URL before the stream starts. How do I have to proceed to realize this project?
Below is the most important part of the code of the simple player:
const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest'
|| (Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'PlayStationIntent');
},
handle(handlerInput) {
const speakOutput = messages.welcome;
return handlerInput.responseBuilder
.speak(speakOutput)
.addAudioPlayerPlayDirective("REPLACE_ALL", url, token(), 0)
.getResponse();
}
};
There are multiple options for implementing this:
The last option would change your code to something like:
const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest'
|| (Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'PlayStationIntent');
},
handle(handlerInput) {
const speakOutput = messages.welcome;
return handlerInput.responseBuilder
.speak(speakOutput)
.addAudioPlayerPlayDirective("REPLACE_ALL", url_jingle, "jingle", 0)
.getResponse();
}
};
const PlayBackFinishedHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'AudioPlayer.PlaybackNearlyFinished';
},
handle(handlerInput) {
if (handlerInput.requestEnvelope.request.token === 'jingle') {
return handlerInput.responseBuilder
.addAudioPlayerPlayDirective("REPLACE_ALL", url, token(), 0)
.getResponse();
}
}
};
Additionally you would need to activate the AudioPlayer Interface and rebuild the model afterwards, so that those controls events will reach you.