小程序激励式视频广告使用

2019-04-22  本文已影响0人  _conquer_

文档:https://wximg.qq.com/wxp/assets/pdf/reward0415.pdf
注意:一个页面只能创建一个广告对象
以下是封装的方法

export default function(adUnitId) {
    this.videoAd = false;   
    
    if(wx.createRewardedVideoAd){
        this.videoAd=wx.createRewardedVideoAd({
            adUnitId:adUnitId
        })
    }
    
    /**
     * @param opt
     */
    this.show = function(opt){
        
        var defaul_opt = {
            show_suc:function(){ console.log('video ad show suc'); },
            show_fail:function(){},
            play_suc:function(){},
            play_fail:function(){}
        }
        
        opt = { ...defaul_opt,...opt };
        
        //on error
        this.videoAd.offError();
        this.videoAd.onError(function(err){
            console.log('video ad on error',err);
        });
        
        //load show 
        var that = this;        
        this.videoAd.show().then(opt.show_suc).catch(function(err){
            that.videoAd.load().then(function(){
                that.videoAd.show().then(opt.show_suc);
            }).catch(opt.show_fail);
        });
        
        //on close
        this.videoAd.offClose();
        this.videoAd.onClose((status)=>{
            if(status && status.isEnded || status === undefined){
                //正常播放结束,可以下发游戏奖励
                opt.play_suc();
            }else{
                //播放中途退出,不下发游戏奖励
                opt.play_fail();
            }
        })
    }
    
}

然后我们直接调用就可以了

that.videoAdObj = new adVideoUtil('adunit-fd6c6f6f5129493f');
//playCoinSuc 播放成功执行的方法
//playError       展示视频失败执行的方法
//play_fail        视频播放中途退出执行的方法
this.videoAdObj.show({
    play_suc:this.playCoinSuc,
    show_fail:this.playError,
    play_fail:this.play_fail
})
上一篇下一篇

猜你喜欢

热点阅读