小度自定义技能播放音频文件(可以暂停/继续)的例子.

参考例子:

https://github.com/dueros/bot-sdk-node.js/blob/master/samples/audio_play/Bot.js

将例子的代码做了一些必要的修改:

1. 增加了this.setExpectSpeech(false);关闭麦克风,不加这句会莫名其妙的停止播放音频.

2. 增加了AudioPlayer.PlaybackFinished事件的处理函数,如果不加则会在播放一遍后自动退出技能.

注意:this.waitAnswer();用于保持技能会话状态,是必须加的,不加会退出.

const Bot = require('bot-sdk');
const privateKey = require("./rsaKeys.js").privateKey;
const Play = Bot.Directive.AudioPlayer.Play;
const Stop = Bot.Directive.AudioPlayer.Stop;

class DuerOSBot extends Bot {
    constructor(postData) {
        super(postData);

        this.addLaunchHandler(() => {
            let card = new Bot.Card.TextCard('欢迎使用动物叫声合成,你可以说开始播放');
            this.waitAnswer();
            return {
                card: card,
                outputSpeech: '欢迎使用动物叫声合成,你可以说开始播放'
            };
        });

        this.addSessionEndedHandler(() => {
            let directive = new Stop();
            this.endSession();
            return {
                directives: [directive],
                outputSpeech: '退出动物叫声合成,欢迎下次再来'
            };
        });

        this.addIntentHandler('audio_play_intent', () => {
            let card = new Bot.Card.TextCard('开始播放动物叫声合成');
            this.waitAnswer();
            this.setExpectSpeech(false);
            return {
                card: card,
                directives: [this.playSound(0)],
                outputSpeech: '开始播放动物叫声合成'
            };
        });

        this.addIntentHandler('ai.dueros.common.pause_intent', () => {
            let card = new Bot.Card.TextCard('已暂停播放');
            let directive = new Stop();
            this.waitAnswer();
            this.setExpectSpeech(false);
            return {
                card: card,
                directives: [directive],
                outputSpeech: '已暂停播放'
            };
        });

        this.addIntentHandler('ai.dueros.common.continue_intent', () => {
            let offsetInMilliSeconds = this.getSessionAttribute('offsetInMilliSeconds',0);
            let card = new Bot.Card.TextCard('继续播放动物叫声合成');
            this.waitAnswer();
            this.setExpectSpeech(false);
            return {
                card: card,
                directives: [this.playSound(offsetInMilliSeconds)],
                outputSpeech: '继续播放动物叫声合成'
            };
        });
        
        this.addEventListener("AudioPlayer.PlaybackStarted", event => {
            this.waitAnswer();
            this.setExpectSpeech(false);
            this.setSessionAttr(event);
        });

        this.addEventListener("AudioPlayer.PlaybackStopped", event => {
            this.waitAnswer();
            this.setExpectSpeech(false);
            this.setSessionAttr(event);
        });
        
        this.addEventListener("AudioPlayer.PlaybackNearlyFinished", event => {
            this.waitAnswer();
            this.setExpectSpeech(false);
            return {
                directives: [this.playSound(0,Play.ENQUEUE)]
            };
        });

        this.addEventListener("AudioPlayer.PlaybackFinished", event => {
            this.waitAnswer();
            this.setExpectSpeech(false);
        });
    }

    playSound(offset, pb = Play.REPLACE_ALL) {
        let directive = new Play('http://...略...', pb);
        directive.setToken('1234567890abcdef');
        if (offset != 0) {
            directive.setOffsetInMilliSeconds(offset);
        }
        return directive;
    }

    setSessionAttr(event) {
        //this.setSessionAttribute('token',event.token);
        this.setSessionAttribute('offsetInMilliSeconds',event.offsetInMilliSeconds);
    }
}

exports.handler = function(event, context, callback) {
    try {
        let b = new DuerOSBot(event);
        // 0: debug  1: online
        b.botMonitor.setEnvironmentInfo(privateKey, 0);
        b.run().then(function(result) {
            callback(null, result);
        }).catch(callback);
    } catch (e) {
        callback(e);
    }
}

该技能已通过审核并上线,可以使用. 

猜你喜欢

转载自blog.csdn.net/milaoshu1020/article/details/85321000