微信小程序激励视频广告onClose多次回调解决方案

51 阅读1分钟

最近写小程序,按照官方指引代码,粘贴进小程序中,会发现onClose会多次回调,官方指引文档又非常简单,没有具体说这个问题,造成的问题是观看多次会重复下发奖励。

这个是我尝试了,没可用,可能是因为我是uni-app开发的

根据祥哥代码:【我是后端真不会,百度没查询到】

let videoAd: any = null
let promiseRes: any = null
let promiseRej: any = null
let playVideo: any = null

let isPlayed = false

const resetPlayState = () => {
    // 显示广告
    isPlayed = false
    playVideo = new Promise((res, rej) => {
            promiseRes = res;
            promiseRej = rej;
    })
}
resetPlayState()   // 必须加

onLoad(() => {
	if (wx.createRewardedVideoAd) {
	  videoAd = wx.createRewardedVideoAd({
	    adUnitId: 'adunit-a46ea070b9ec5d57'
	  })
	  videoAd.onLoad(() => {
		  console.log('load')
	  })
	  videoAd.onError((err) => {
            promiseRej() // 必须加
	    console.error('激励视频光告加载失败', err)
	  })
	  videoAd.onClose((res) => {
		  if(res.isEnded) {
			promiseRes() // 必须加
			isPlayed = true // 必须加
		  } else {
			  uni.showToast({
			  	icon: 'none',
				title: '需广告观看后方可获取数据!',  // 强制打断提醒话语
			  })
		  }
	  })
	}

})

调用代码:

const addjf = async() => {
	resetPlayState()    // 必须加
	try {
		videoAd.show()
		await playVideo
	} catch (err) {
		isPlayed = false
	}
};


可以短暂的解决 激励视频广告 声音异常现象。

这个也尝试了下,有效果,但是刷新刷新还是会出现,唉上火