文檔: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
})