uniapp中对接开发激励广告视频

官网地址:激励视频广告 | uni-app官网

我开发的app激励视频广告使用的是api的方式生成激励视频

核心语法

官方示例代码

复制代码
rewardedVideoAd.onLoad(() => {
  console.log('激励视频 广告加载成功')
})

rewardedVideoAd.show()
.then(() => console.log('激励视频 广告显示'))

app要传的是adpid,在hbulider广告联盟后台创建自己的adpid,

微信小程序要传的是adUnitId,在微信小程序的广告主后台获取这个值

复制代码
	showCustomAd(redPacket) {
				if (this.isVideoPlaying || !this.isPageAlive) {
					this.showToast("视频播放中,请稍候");
					return;
				}
				const that = this;
				const {
					adpid
				} = this.base.groupConfig;

				that.resetAdData();
				that.isVideoPlaying = true;

				uni.showLoading({
					title: "加载中...",
					mask: true
				});

				const orderNo = that.generateOrderNo();

				let adInstance = null;

              //生成app的激励视频
				//#ifdef APP-PLUS
				adInstance = uni.createRewardedVideoAd({
					adpid: adpid,
					urlCallback: {
						userId: uni.getStorageSync('userInfo').uid || '',
						extra: JSON.stringify({
							type: 2,
							no: orderNo,
							box: redPacket.source === "treasureChest" ? 1 : ''
						})
					}
				});
				//#endif


                //生成微信小程序的激励视频
				//#ifdef MP-WEIXIN
				adInstance = uni.createRewardedVideoAd({
					adUnitId: adpid,
				});
				//#endif

				that.ad.instance = adInstance;
				that.ad.currentRedPacket = redPacket; //红包
				that.ad.orderNo = orderNo; //提交订单号去后台

				adInstance.onLoad(() => {
					uni.hideLoading();
					setTimeout(() => {
						adInstance.show()
					}, 200)

				});

				adInstance.onError((err) => {
					uni.hideLoading();
					that.isVideoPlaying = false;
					that.cleanupAdInstance();
				});

				adInstance.onClose((res) => {
					uni.hideLoading();
					that.isVideoPlaying = false;
					if (res && res.isEnded) {
						that.handleVideoFinish(that.ad.currentRedPacket, that.ad.orderNo);
					} else {
						that.showToast("中途退出,领取失败");
					}

					//#ifdef APP-PLUS
					// 清理广告实例
					that.cleanupAdInstance();
					//#endif
				});

				//#ifdef MP-WEIXIN
				adInstance.show().catch(err => {
					adInstance.load().then(() => {
						return adInstance.show();
					}).catch(err2 => {
						uni.hideLoading();
						that.isVideoPlaying = false;
					});
				});
				//#endif
			},

提示:在正式开始之前,一定要多去看看官方文档,看看他们的写法和api的用法, 不要直接上手就去写,很浪费时间,而且用法不对,还会一直报错!多看文档多看文档!!!!

相关推荐
小恒恒1 天前
2025 Vibe Coding 有感
前端·uni-app·trae
一颗小青松1 天前
uniapp使用uni-im
uni-app
2501_916007472 天前
iPhone APP 性能测试怎么做,除了Instruments还有什么工具?
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 天前
Windows 环境下有哪些可用的 iOS 上架工具, iOS 上架工具的使用方式
android·ios·小程序·https·uni-app·iphone·webview
一颗小青松2 天前
uniapp vue3中app端使用腾讯云点播上传
uni-app·云计算·腾讯云
玄尺_0072 天前
uniapp h5端使浏览器弹出下载框
前端·javascript·uni-app
2501_915106322 天前
iOS 抓包工具有哪些?不同类型的抓包工具可以做什么
android·ios·小程序·https·uni-app·iphone·webview
web前端神器2 天前
vue、uniapp项目循环中能显示每个列表的内容,但是点击的时候传递的参数却不正确
uni-app
ModyQyW2 天前
HBuilderX 4.87 无法正常读取 macOS 环境配置的解决方案
前端·uni-app
脾气有点小暴2 天前
Uni-app App 端自定义导航栏完整实现指南
uni-app