小程序激勵式視頻廣告使用

文檔:https://wximg.qq.com/wxp/assets/pdf/reward0415.pdf
注意:一個頁面只能創(chuàng)建一個廣告對象
以下是封裝的方法

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){
                //正常播放結(jié)束,可以下發(fā)游戲獎勵
                opt.play_suc();
            }else{
                //播放中途退出,不下發(fā)游戲獎勵
                opt.play_fail();
            }
        })
    }
    
}

然后我們直接調(diào)用就可以了

  • 首先我們在onload的時候創(chuàng)建一個廣告對象
that.videoAdObj = new adVideoUtil('adunit-fd6c6f6f5129493f');
  • 然后我們點擊的時候進行播放
//playCoinSuc 播放成功執(zhí)行的方法
//playError       展示視頻失敗執(zhí)行的方法
//play_fail        視頻播放中途退出執(zhí)行的方法
this.videoAdObj.show({
    play_suc:this.playCoinSuc,
    show_fail:this.playError,
    play_fail:this.play_fail
})
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容