使用Nodejs BOT SDK开发音频播放技能示例代码
官
音频播放技能依次播放列表中的视频,并支持“暂停”,“继续”,“上一个”,“下一个”等常用音频指令。
意图配置:
示例代码:
const Bot = require('bot-sdk');
const privateKey = require("./rsaKeys.js").privateKey;
const musics = [
{
token: 'a0b923820dcc509a',
url: 'xxx',
singer: '歌手名',
name: '歌曲名'
},
{
token: '9d4c2f636f067f89',
url: 'xxx',
singer: '歌手名',
name: '歌曲名'
},
{
token: '4b5ce2fe28308fd9',
url: 'xxx',
singer: '歌手名',
name: '歌曲名'
},
{
token: 'a2f3e71d9181a67b',
url: 'xxx',
singer: '歌手名',
name: '歌曲名'
}
];
const default_image = 'https://skillstore.cdn.bcebos.com/icon/100/c709eed1-c07a-be4a-b242-0b0d8b777041.jpg';
class InquiryBot extends Bot {
constructor(postData) {
super(postData);
this.curIndex = 0;
this.waitAnswer();
this.addLaunchHandler(() => {
this.waitAnswer();
this.setExpectSpeech(false);
return {
directives: [this.getDirective(0)],
outputSpeech: '欢迎使用音乐播放器!',
};
});
this.addIntentHandler('audio_play_intent', () => {
this.waitAnswer();
this.setExpectSpeech(false);
return {
directives: [this.getDirective(0), this.getTemplate2(musics[curIndex])],
outputSpeech: '正在为你播放'
};
});
// system pause intent
this.addIntentHandler('ai.dueros.common.pause_intent', () => {
let directive = new Bot.Directive.AudioPlayer.Stop();
this.waitAnswer();
this.setExpectSpeech(false);
return {
directives: [directive, this.getTemplate2(musics[this.curIndex])],
outputSpeech: '已经暂停播放'
};
});
// system continue intent
this.addIntentHandler('ai.dueros.common.continue_intent', () => {
let token = this.getSessionAttribute('token');
let offsetInMilliSeconds = this.getSessionAttribute('offsetInMilliSeconds');
this.updateCurrentSingIndex(token);
this.waitAnswer();
this.setExpectSpeech(false);
return {
directives: [this.getDirective(offsetInMilliSeconds), this.getTemplate2(musics[this.curIndex])],
outputSpeech: `继续播放${musics[this.curIndex].name}`
};
});
// system next intent
this.addIntentHandler('ai.dueros.common.next_intent', () => {
let token = this.getSessionAttribute('token');
this.updateNextSingIndex(token);
this.waitAnswer();
this.setExpectSpeech(false);
return {
directives: [this.getDirective(0), this.getTemplate2(musics[this.curIndex])],
outputSpeech: `播放下一首:${musics[this.curIndex].name}...`
};
});
// system previous intent
this.addIntentHandler('ai.dueros.common.previous_intent', () => {
this.waitAnswer();
this.setExpectSpeech(false);
let token = this.getSessionAttribute('token');
this.updatePreviousSingIndex(token);
return {
directives: [this.getDirective(0), this.getTemplate2(musics[this.curIndex])],
outputSpeech: `播放上一首:${musics[this.curIndex].name}...`
};
});
// AudioPlayer.PlaybackStarted
this.addEventListener('AudioPlayer.PlaybackStarted', event => {
this.waitAnswer();
this.setExpectSpeech(false);
this.setSessionAttr(event);
});
// client event PlaybackStopped
this.addEventListener('AudioPlayer.PlaybackStopped', event => {
this.waitAnswer();
this.setExpectSpeech(false);
this.setSessionAttr(event);
});
this.addEventListener('AudioPlayer.PlaybackFinished', event => {
this.waitAnswer();
this.setExpectSpeech(false);
let token = this.getSessionAttribute('token');
this.updateNextSingIndex(token);
this.waitAnswer();
this.setExpectSpeech(false);
return {
directives: [this.getDirective(0), this.getTemplate2(musics[this.curIndex])],
outputSpeech: `播放下一首:${musics[this.curIndex].name}...`
};
});
this.addDefaultEventListener(function () {
this.waitAnswer();
this.setExpectSpeech(false);
});
this.addIntentHandler('ai.dueros.common.default_intent', () => {
this.waitAnswer();
return {
outputSpeech: '请对我说开始来播放音频。您也可以跟我说退出来退出技能。'
};
});
this.addSessionEndedHandler(() => {
let directive = new Bot.Directive.AudioPlayer.Stop();
return {
directives: [directive, this.getTemplate2('退出音乐播放,欢迎下次再来!')],
outputSpeech: '退出音乐播放,欢迎下次再来!'
};
});
}
/**
* 更新下一曲的index
*
* @param {string} token 歌曲的id
*/
updateNextSingIndex(token) {
let self = this;
musics.map((music, i) => {
if (music.token === token) {
self.curIndex = parseInt(i, 10) + 1 <= musics.length - 1 ? parseInt(i, 10) + 1 : musics.length - 1;
}
return null;
});
}
/**
* 更新上一首的index
*
* @param {string} token 歌曲的id
*/
updatePreviousSingIndex(token) {
let self = this;
musics.map((music, i) => {
if (music.token === token) {
self.curIndex = parseInt(i, 10) - 1 >= 0 ? parseInt(i, 10) - 1 : 0;
}
return null;
});
}
/**
* 更新当前正在播放歌曲的index
*
* @param {string} token 歌曲的id
*/
updateCurrentSingIndex(token) {
let self = this;
musics.map(function (music, i) {
if (music.token === token) {
self.curIndex = parseInt(i, 10);
}
return null;
});
}
/**
* 获取歌曲播放指令
*
* @param {number} offset 歌曲播放的进度
* @return {Bot.Directive.AudioPlayer.Play} Play指令
*/
getDirective(offset = 0) {
let directive = new Bot.Directive.AudioPlayer.Play(musics[this.curIndex].url);
directive.setToken(musics[this.curIndex].token);
this.setSessionAttribute('token', musics[this.curIndex].token);
directive.setOffsetInMilliSeconds(offset);
return directive;
}
/**
* 获取上图下文模版
*
* @param {Object} music 歌曲详情
* @return {RenderTemplate} 渲染模版
*/
getTemplate2(music) {
let bodyTemplate = new Bot.Directive.Display.Template.BodyTemplate2();
bodyTemplate.setToken(music.token);
bodyTemplate.setBackGroundImage(default_image);
bodyTemplate.setTitle(music.singer);
bodyTemplate.setPlainContent(music.name);
let renderTemplate = new Bot.Directive.Display.RenderTemplate(bodyTemplate);
return renderTemplate;
}
/**
* 获取文本展现模板
*
* @param {string} text 歌曲详情
* @return {RenderTemplate} 渲染模版
*/
getTemplate1(text) {
let bodyTemplate = new Bot.Directive.Display.Template.BodyTemplate1();
bodyTemplate.setPlainTextContent(text);
let renderTemplate = new Bot.Directive.Display.RenderTemplate(bodyTemplate);
return renderTemplate;
}
/**
* 设置会话属性
*
* @param {Object} event 客户端上报的事件,包含歌曲token和对应播放进度等信息
*/
setSessionAttr(event) {
this.setSessionAttribute('token', event.token);
this.setSessionAttribute('offsetInMilliSeconds', event.offsetInMilliSeconds);
}
}
exports.handler = function (event, context, callback) {
try {
let b = new InquiryBot(event);
// 0: debug 1: online
b.botMonitor.setEnvironmentInfo(privateKey, 0);
b.run().then(function (result) {
callback(null, result);
}).catch(callback);
} catch (e) {
callback(e);
}
}
点赞
(
1
)
收藏
TOP
棒棒哒
棒棒哒
问题还存在么?
棒棒哒
是什么设备?技能id告知一下?
要不加一下开发qq群151767001,在里面跟我们联系?
棒棒哒
棒棒哒
请问这个Token是用来标识当前资源的token怎么生成的呢?
想问一下,小度在家有没有本地缓存的机制?
token开发者自己定义就好了