如下图 就是用户偶尔会播放广告失败。后续 任何广告都是这个失败。只能重启小游戏 才会好。
export default class WXMiniAd {
public rewardedVideoAd: wx.RewardedVideoAd = null;
public adLoadError: boolean = false;
public adErrorCode: number = 0;
public adErrorReason: string = "";
private adUnitId: string = "";
public func_初始化(opt: {
adUnitId: string,
}) {
let { adUnitId } = opt;
this.adUnitId = adUnitId;
// 创建激励视频广告全局实例,提前初始化
this.rewardedVideoAd = wx.createRewardedVideoAd({
adUnitId: adUnitId,
multiton: true,
});
this.rewardedVideoAd.onError((err) => {
this.adErrorCode = err.errCode;
this.adLoadError = true;
this.adErrorReason = err.errMsg + "(" + err.errCode + ")";
console.error('激励视频 广告显示失败', err);
let msg = err.errMsg;
if (err.errCode) {
msg = `${msg}(${err.errCode})`
}
WXMiniGameSdk.showDebugModal({ content: msg } as any);
});
return this;
}
private resolve: Function;
public async PlayAd(opt: {
showLoading?: boolean,
showError?: boolean,
}): Promise<{
res: 0 | 1,
errMsg: string,
}> {
if (DebugMode) {
WXMiniGameSdk.showDebugModal({ content: `广告位:${this.adUnitId}` } as any);
}
let { showLoading, showError } = opt;
if (this.adLoadError) {
if (showError) {
let errMsg = ADError[this.adErrorCode] || `广告加载失败请稍后再试`;
let msg = `${errMsg}`;
if (this.adErrorCode) {
msg = `${errMsg}(${this.adErrorCode})`;
} else {
this.rewardedVideoAd.load();
}
wx.showModal({ content: msg } as any);
} else {
WXMiniGameSdk.showDebugModal({ content: this.adErrorReason } as any);
}
return {
res: 0,
errMsg: "广告加载失败",
};
}
return new Promise(resolve => {
this.resolve = resolve;
if (showLoading) {
wx.showLoading({
title: '广告加载中...',
});
}
//广告显示成功
this.rewardedVideoAd.offClose(null);
this.rewardedVideoAd.onClose(this.onAdClose.bind(this));
this.rewardedVideoAd.show()
.catch(() => {
this.rewardedVideoAd.load();
if (showLoading) {
wx.hideLoading();
}
return resolve({
res: 0,
errMsg: "广告未就绪,请稍后再试",
});
}).then(() => {
//广告展示成功
if (showLoading) {
wx.hideLoading();
}
});
});
}
private onAdClose(res: { isEnded: boolean }) {
this.rewardedVideoAd.load();
// 用户点击了【关闭广告】按钮
// 小于 2.1.0 的基础库版本,res 是一个 undefined
if (res && res.isEnded || res === undefined) {
// 正常播放结束,可以下发游戏奖励
// this.奖励();
return this.resolve({
res: 1,
errMsg: "播放成功",
});
}
else {
// 播放中途退出,不下发游戏奖励
return this.resolve({
res: 0,
errMsg: "播放中途退出,不下发游戏奖励",
});
}
}
}
弄了个同行业圈子,解决问题交流更方便些,要进的朋友可以联系我
联系微信:LX959996