找回密码
 立即注册
搜索

激励视频广告调用show方法报错1001

1
回复
377
查看
[复制链接]

46

主题

47

帖子

465

积分

 楼主| 2021-6-25 16:33:52 显示全部楼层 |阅读模式

在激励视频广告展示过程中,如果点击关闭激励视频广告,然后直接调用show方法展示广告会报错{"errCode":1001,"errMsg":"Parameter error."}

错误代码如下:

<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.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>


回复

使用道具 举报

23

主题

142

帖子

825

积分

2021-6-25 16:36:36 显示全部楼层

问题分析

问题在于激励视频广告调用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


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册