视频播放技能依次播放列表中的视频,并支持“暂停”,“继续”,“上一个”,“下一个”等常用视频指令。
意图配置:
示例代码:
const Bot = require('bot-sdk');
const privateKey = require('./rsaKeys.js').privateKey;
const player = Bot.Directive.VideoPlayer.Play;
let curIndex = 0;
const videoList = [
'url_1',
'url_2'
];
class InquiryBot extends Bot {
constructor(postData) {
super(postData);
this.addLaunchHandler(() => {
this.waitAnswer();
this.setExpectSpeech(false);
let directive = this.getVideoPlayer(videoList[curIndex], curIndex);
let playerInfo = this.getPlayerInfo(curIndex);
return {
directives: [directive, playerInfo],
outputSpeech: '欢迎进入视频播放。'
};
});
this.addSessionEndedHandler(() => {
this.endSession();
return {
outputSpeech: '再见!'
};
});
this.addIntentHandler('ai.dueros.common.continue_intent', () => {
this.waitAnswer();
this.setExpectSpeech(false);
let offset = this.getSessionAttribute('offsetInMilliSeconds');
let directive = this.getVideoPlayer(videoList[curIndex], curIndex);
let playerInfo = this.getPlayerInfo(curIndex);
directive.setOffsetInMilliseconds(offset);
return {
directives: [directive, playerInfo],
};
});
this.addIntentHandler('ai.dueros.common.next_intent', () => {
this.waitAnswer();
this.setExpectSpeech(false);
curIndex++;
if (curIndex >= videoList.length) {
curIndex = 0;
}
let directive = this.getVideoPlayer(videoList[curIndex], curIndex);
let playerInfo = this.getPlayerInfo(curIndex);
return {
directives: [directive, playerInfo],
};
});
this.addIntentHandler('ai.dueros.common.previous_intent', () => {
this.waitAnswer();
this.setExpectSpeech(false);
if (curIndex > 0) {
curIndex--;
}
let directive = this.getVideoPlayer(videoList[curIndex], curIndex);
let playerInfo = this.getPlayerInfo(curIndex);
return {
directives: [directive, playerInfo],
};
});
this.addIntentHandler('start', () => {
this.waitAnswer();
this.setExpectSpeech(false);
curIndex = 0;
let directive = this.getVideoPlayer(videoList[curIndex], curIndex);
let playerInfo = this.getPlayerInfo(curIndex);
return {
directives: [directive, playerInfo],
};
});
this.addIntentHandler('ai.dueros.common.pause_intent', () => {
this.waitAnswer();
this.setExpectSpeech(false);
let directive = new Bot.Directive.VideoPlayer.Stop();
return {
directives: [directive],
};
});
this.addIntentHandler('ai.dueros.common.stop_intent', () => {
this.waitAnswer();
this.setExpectSpeech(false);
let directive = new Bot.Directive.VideoPlayer.Stop();
return {
directives: [directive],
};
});
this.addIntentHandler('ai.dueros.common.default_intent', () => {
this.waitAnswer();
return {
outputSpeech: '请对我说开始或者继续来播放视频。您也可以跟我说退出来退出技能。'
};
});
this.addEventListener('VideoPlayer.PlaybackStopped', function (event) {
this.waitAnswer();
this.setExpectSpeech(false);
this.setSessionAttribute('offsetInMilliSeconds', event.offsetInMilliseconds);
return {
outputSpeech: '好的'
};
});
this.addEventListener('VideoPlayer.PlaybackFinished', function (event) {
this.waitAnswer();
this.setExpectSpeech(false);
curIndex++;
if (curIndex >= videoList.length) {
curIndex = 0;
}
let directive = this.getVideoPlayer(videoList[curIndex], curIndex);
let playerInfo = this.getPlayerInfo(curIndex);
return {
directives: [directive, playerInfo]
};
});
this.addEventListener('Form.ButtonClicked', function (event) {
this.waitAnswer();
this.setExpectSpeech(false);
let buttonName = event.name;
if (buttonName == 'NEXT') {
curIndex++;
if (curIndex >= videoList.length) {
curIndex = 0;
}
let directive = this.getVideoPlayer(videoList[curIndex], curIndex);
let playerInfo = this.getPlayerInfo(curIndex);
return {
directives: [directive, playerInfo],
};
} else if (buttonName == 'PREVIOUS') {
if (curIndex > 0) {
curIndex--;
}
let directive = this.getVideoPlayer(videoList[curIndex], curIndex);
let playerInfo = this.getPlayerInfo(curIndex);
return {
directives: [directive, playerInfo],
};
}
});
this.addDefaultEventListener(function () {
this.waitAnswer();
this.setExpectSpeech(false);
});
}
/**
* 获取视频播放指令
*
* @param {number} offset 歌曲播放的进度
* @return {Bot.Directive.VideoPlayer.Play} Play指令
*/
getVideoPlayer(videoUrl, token) {
let directive = new Bot.Directive.VideoPlayer.Play(videoUrl, player.REPLACE_ALL);
directive.setToken(token.toString());
return directive;
}
/**
* 获取播放控制控件
*
* @return {RenderVideoPlayerInfo} RenderVideoPlayerInfo
*/
getPlayerInfo(token) {
let playerInfo = new Bot.Directive.Display.RenderVideoPlayerInfo();
playerInfo.addControl(new Bot.Directive.AudioPlayer.Control.PreviousButton());
playerInfo.addControl(new Bot.Directive.AudioPlayer.Control.PlayPauseButton());
playerInfo.addControl(new Bot.Directive.AudioPlayer.Control.NextButton());
playerInfo.setToken(token.toString());
return playerInfo;
}
}
exports.handler = function (event, context, callback) {
try {
let b = new InquiryBot(event);
// 0: debug 1: online
b.botMonitor.setEnvironmentInfo(privateKey, 1);
b.run().then(function (result) {
callback(null, result);
}).catch(callback);
} catch (e) {
callback(e);
}
}
请登录后评论
TOP
点赞
赞,如果能使用音频、视频流形式就更完美了
如何监听一个视频即将播放完毕?我想自动播放下一个
谢谢!
这些接口有文档吗?我在视频里看到了接口的说明,但是没有找到对应的页面,有人可以提供一下吗?多谢!
视频播放说明 https://dueros.baidu.com/didp/doc/dueros-bot-platform/dbp-custom/videoplayer_markdown
SDK文档:https://github.com/dueros/bot-sdk-node.js/blob/master/doc/directive/VideoPlayer/Play.md
我的团长我的团是1个资源,需要把语音上一集/第N集/下一集转换为指令
比如:第5集转为指令ChoseEpisode,参数episodeNumber5,然后把指令ChoseEpisode和参数episodeNumber给视频播放客户端去执行
请问,怎么实现?
您这个可以设置一个意图,该意图的常用表达可以是“第5集”,“播放第5集”等等。然后对5这个槽位引入系统词典sys.number。意图被唤醒以后获取槽位值,返回相应播放指令。
您可以参照 https://dueros.baidu.com/forum/topic/show/293365 中对槽位的处理。
那个触摸屏上面视频播放器的下一个、上一个和播放列表分别对应什么事件呢?
建议多看文档 Form事件
技能在小度在家上体验,技能不支持“上/下一个”“播放列表”等点击操作,需要支持,示例代码应该完善啊,要不然不给通过。
1、建议多看文档 Form事件
2、addEventListener注册Form对应事件回调方法,方法内通过event中name字段获取控件名, 之后处理自己的业务逻辑
3、伪代码:
我添加了事件,但是播放完成没触发
更新了代码。在下发Play指令的同时,也同时下发Bot.Directive.Display.RenderVideoPlayerInfo指令。还增加了Form.ButtonClicked事件的处理。
另外需要注意,Play指令和RenderVideoPlayerInfo指令需要设置相同的Token才能正常工作。
播放列表应该怎么获取和展现?
点击菜单按钮并没有打开菜单
审核不通过,过来看,才发现,刚刚示例代码有不少更新
另外也提供 用户点击视频列表按钮后展现列表的全部代码