Build a custom audio stream (from a video url) for Echo devices
I needed to play an italian television news only channel (Rai News 24) on my Echos devices without a display, but there wasn’t the skill to do it, so I built my own using the .m3u8 official stream from the channel, yes the stream with video, works as audio only on Echo devices.
And this is a simply tutorial to build the same if you are italian, or another straming radio/channel/tv with a custom stream url (I think works only for .m3u8 streams but you can try with other formats). Start:
Create a new developer account from developer.amazon.com, the account must be the same as the Echo account.
Create a new skill Alexa-Hosted (Node.js) called with your custom name (mine is “Rai News 24 Audio”).
Navigate under “Code” and paste this code:
const Alexa = require('ask-sdk-core');var https = require('https');const PlayHandler = {canHandle(handlerInput){return (handlerInput.requestEnvelope.request.type === 'LaunchRequest' ||(handlerInput.requestEnvelope.request.type === 'IntentRequest' &&handlerInput.requestEnvelope.request.intent.name === 'Play') ||(handlerInput.requestEnvelope.request.type === 'IntentRequest' &&handlerInput.requestEnvelope.request.intent.name === 'AMAZON.ResumeIntent'));},handle(handlerInput){return handlerInput.responseBuilder.addDirective({type: 'AudioPlayer.Play',playBehavior: 'REPLACE_ALL',audioItem:{stream:{token: '0',url: 'https://rainews1-live.akamaized.net/hls/live/598326/rainews1/rainews1/playlist.m3u8',offsetInMilliseconds: 0}}}).getResponse();}};const PauseStopHandler = {canHandle(handlerInput){return (handlerInput.requestEnvelope.request.type === 'IntentRequest' &&(handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent' ||handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent')) ||(handlerInput.requestEnvelope.request.type === 'IntentRequest' &&handlerInput.requestEnvelope.request.intent.name === 'AMAZON.PauseIntent');},handle(handlerInput){return handlerInput.responseBuilder.addDirective({type: 'AudioPlayer.ClearQueue',clearBehavior: 'CLEAR_ALL'}).getResponse();}};const HelpIntentHandler = {canHandle(handlerInput){return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';},handle(handlerInput){const speechText = 'Puoi dire riproduci, stop o riprendi. Per maggiori informazioni vai al sito di RaiNews24 punto it';return handlerInput.responseBuilder.speak(speechText).getResponse();}};const SessionEndedRequestHandler = {canHandle(handlerInput){return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';},handle(handlerInput){return handlerInput.responseBuilder.getResponse();}};const IntentReflectorHandler = {canHandle(handlerInput){return handlerInput.requestEnvelope.request.type === 'IntentRequest';},handle(handlerInput){const intentName = handlerInput.requestEnvelope.request.intent.name;const speechText = 'NO INTENT HELP TEXT';return handlerInput.responseBuilder.speak(speechText).getResponse();}};const ErrorHandler = {canHandle(){return true;},handle(handlerInput, error){const speechText = `Sorry, I could not understand what you said. Please try again.`;return handlerInput.responseBuilder.speak(speechText).reprompt(speechText).getResponse();}};exports.handler = Alexa.SkillBuilders.custom().addRequestHandlers(PlayHandler,PauseStopHandler,HelpIntentHandler,SessionEndedRequestHandler,IntentReflectorHandler).addErrorHandlers(ErrorHandler).lambda();
Put your custom url on the line 28:
url: ‘https://rainews1-live.akamaized.net/hls/live/598326/rainews1/rainews1/playlist.m3u8',
Then tap on “Deploy”.
Back in the “Build” tab, go to “Interaction Model” > “JSON Editor” and paste this code with your custom voice commands obviously
{"interactionModel": {"languageModel": {"invocationName": "rai news audio","intents": [{"name": "AMAZON.CancelIntent","samples": ["Annulla","Cancel"]},{"name": "AMAZON.HelpIntent","samples": ["Help"]},{"name": "AMAZON.StopIntent","samples": ["Ferma la riproduzione","Stop"]},{"name": "Play","slots": [],"samples": ["metti rai news audio","riproduci rai news audio","chiedi a rai news audio di riprodurre","avvia rai news audio"]},{"name": "AMAZON.NavigateHomeIntent","samples": []},{"name": "AMAZON.PauseIntent","samples": []},{"name": "AMAZON.ResumeIntent","samples": []},{"name": "AMAZON.HelpIntent","samples": []}],"types": []}}}
Tap on “Build model”.
Go under “Interfaces” and activate only “Audio Player” and “Auto Delegation” (press “Save” after done).
Tap again on “Build Model”
Wait untill the end and it’s done. Should works in few minutes by invoke with your custom voice command (yes you can also use the GUI inputs, is the same).
Bonus: you will have also your custom stats under the “Analytics” tab =)
You can try to submit to the Alexa Store, but I don’t think it will be accepted because you are not the owner of Rai Television account/channel (or other sources). If you put your radio stream, maybe yes. But I’ve already tried to submit it and it has been declined.