使用Nodejs BOT SDK开发视频播放技能示例代码
D****超 · D****超 发布于2018-11-17 16:47 浏览:5041 回复:43

视频播放技能依次播放列表中的视频,并支持“暂停”,“继续”,“上一个”,“下一个”等常用视频指令。

意图配置:

示例代码:

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);
    }
}
点赞  ( 6 )
收藏
评论(43)
共43条回复 最后由王****e回复于2019-11-12 18:55
#2h****g回复于2018-11-17 21:22:52

点赞

0
#3q****i回复于2018-11-18 09:10:01

赞,如果能使用音频、视频流形式就更完美了

0
#4n****8回复于2018-11-18 11:05:10

如何监听一个视频即将播放完毕?我想自动播放下一个

0
#5回复于2018-11-18 21:10:42
#4 n****8回复
如何监听一个视频即将播放完毕?我想自动播放下一个
VideoPlayer.PlaybackFinished
0
#6n****8回复于2018-11-18 21:17:17
#5 回复
[代码]

谢谢!

0
#7守****v回复于2018-11-20 10:43:19

棒棒哒

0
#8D****超回复于2018-11-20 23:06:03
#7 守****v回复
棒棒哒

棒棒哒

0
#9_****畅回复于2018-11-21 11:22:22

棒棒哒

0
#10D****超回复于2018-11-22 10:24:45
#9 _****畅回复
棒棒哒

棒棒哒

0
#11n****8回复于2018-11-26 21:31:15

那个触摸屏上面视频播放器的下一个、上一个和播放列表分别对应什么事件呢?

0
TOP