问题分析 问题在于激励视频广告调用show展示前,需要先完成预加载操作,否则无法请求到对应的广告资源,也就无法展示广告。 方法是在onClose监听激励视频关闭方法中,加入load方法,这样在关闭激励视频广告时,就能够预先加载下次需要的广告资源,需要展示时直接调用show方法即可。 如下所示: rewardedVideoAd.onClose(res => { console.log("rewarded ad onClose is watch end: " + res.isEnded); if (res.isEnded) { prompt.showToast({ message: "Watch video show finished , add 5 scores", duration: 2000, gravity: "center" }); this.score += 5; } //下一次广告预加载 rewardedVideoAd.load(); //此处添加预加载代码 }); rewardedVideoAd.load(); }, 解决方案 ux文件代码: <template> <div class="item-container"> <text class="alert">This is Rewarded ad demo</text> <text class="alert">Score:{{ score }}</text> <input type="button" class="btn" value="Play" onclick="play" /> <input type="button" class="btn" value="Watch Video" onclick="rewardAdShow" /> </div> </template> <style> .alert { font-size: 40px; margin-top: 20px; margin-bottom: 20px; } .item-container { margin-top: 50px; padding: 20px; flex-direction: column; align-items: center; align-content: center; } .btn { height: 80px; width: 60%; background-color: #00bfff; color: #ffffff; border-radius: 20px; margin-bottom: 20px; } .btn:active { background-color: #058fbd; } </style> <script> import ad from "@service.ad"; import prompt from "@system.prompt"; let rewardedVideoAd; export default { data: { provider: "", score: 0, rewarded: { adUnitId: "testx9dtjwj8hp", errStr: "" } }, onInit() { this.$page.setTitleBar({ text: "Rewarded Ad" }); }, onReady(options) { this.loadRewardAd(); }, getAdProvider: function () { this.provider = ad.getProvider(); prompt.showToast({ message: "getProvider : " + this.provider, duration: 2000, gravity: "center" }); }, loadRewardAd() { this.getAdProvider(); if (this.provider !== "huawei") { console.info("the device does not support ad."); return; } rewardedVideoAd = ad.createRewardedVideoAd({ adUnitId: this.rewarded.adUnitId }); rewardedVideoAd.onLoad(() => { this.rewarded.errStr = ""; prompt.showToast({ message: "onRewardedLoaded", duration: 2000, gravity: "center" }); }); rewardedVideoAd.onError(e => { console.error("load rewarded video ad error:" + JSON.stringify(e)); this.rewarded.errStr = JSON.stringify(e); }); rewardedVideoAd.onClose(res => { console.log("rewarded ad onClose is watch end: " + res.isEnded); if (res.isEnded) { prompt.showToast({ message: "Watch video show finished , add 5 scores", duration: 2000, gravity: "center" }); this.score += 5; } // rewardedVideoAd.offError(() => { console.log('offError'); }) // rewardedVideoAd.offLoad(() => { console.log('offLoad'); }) // rewardedVideoAd.destroy(); //下一次广告预加载 rewardedVideoAd.load(); }); rewardedVideoAd.load(); }, onDestroy() { rewardedVideoAd && rewardedVideoAd.destroy(); }, rewardAdShow: function () { rewardedVideoAd.show(); }, play: function () { if (this.score === 0) { prompt.showToast({ message: "Watch video ad to add score", duration: 2000, gravity: "center" }); return; } let randomNum = parseInt(Math.random(), 1); console.info("play randomNum= " + randomNum); if (randomNum === 1) { this.score += 1; prompt.showToast({ message: 'you win!', duration: 2000, gravity: 'center' }) } else { this.score -= 2; if (this.score < 0) { this.score = 0; prompt.showToast({ message: 'you lose!', duration: 2000, gravity: 'center' }) } } }, }; </script> 欲了解更多详情,请参见: 接入广告介绍: https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickapp-access-ads-kit#h2-1621507173212-0
|