123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- const GameModule = require("../utils/GameModule");
- const SkillApi = require('../net/SkillApi');
- // const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
- const {GameNotificationKey, ADVideoLoadState} = require('../utils/GameEnum');
- class ADVideo {
- // constructor() {
- // this.isNeedShowErr = false;
- // }
- wxCreateADVideo() {
- let defualtADdId = 'adunit-9d6b728b8e8a52d3';
- let videoAd = wx.createRewardedVideoAd({
- adUnitId: defualtADdId
- });
- /// 默认是加载成功的 啦
- GameGlobal._adVideoState = ADVideoLoadState.success;
- this.videoAd = videoAd;
- videoAd.load()
- .then(() => {
- this.isFirstLoad = false;
- GameGlobal._adVideoState = ADVideoLoadState.success;
- GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.success);
- console.log('视频加载成功');
- })
- .catch(err => {
- // if ((err.errMsg && err.errMsg.includes('fetch advertisement failed')) || (err.errCode && err.errCode == 1004)) {
- // Global.commonAlert.showCommonErrorAlert('今日份视频已经播放完啦~~');
- // } else {
- // Global.commonAlert.showCommonErrorAlert('获取视频失败');
- // }
- this.handelVideoErr(err);
- this.isFirstLoad = false;
- });
- // videoAd.onLoad(() => {
- // /// 视频加载成功
- // Global._adVideoState = ADVideoLoadState.success;
- // GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.success);
- // console.log('视频加载成功');
- // });
- // 处理获取视频错误
- videoAd.onError( (res) => {
- this.handelVideoErr(res);
- });
-
- videoAd.onClose((res) => {
- if (res && res.isEnded || res === undefined) {
- console.log('视频正常关闭');
- GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.closeFinish, this.callBackObjct);
- } else {
- console.log('视频未正常关闭');
- GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.closeFail, this.callBackObjct);
- }
- if (GameModule.audioMng) {
- GameModule.audioMng.playBgm();
- }
- SkillApi.report(1, (responseData) => {
- },(error) => {
- });
- });
- }
- static qqPlayADVideo(error,close) {
- var isFinished = false;
- var videoAd = BK.Advertisement.createVideoAd();
- videoAd.onLoad(() => {
- //加载成功
- BK.Script.log(1,1,"onLoad")
- });
- videoAd.onPlayStart(() => {
- if (GameModule.audioMng) {
- GameModule.audioMng.stopBgm();
- }
- //开始播放
- BK.Script.log(1,1,"onPlayStart")
- });
- videoAd.onPlayFinish(() => {
- //播放结束
- BK.Script.log(1,1,"onPlayFinish")
- isFinished = true;
- });
- videoAd.onError((err) => {
- //加载失败
- BK.Script.log(1,1,"onError code:"+err.code+" msg:"+err.msg);
- GameGlobal.commonAlert.showCommonErrorAlert('获取视频失败');
- error && error();
- });
- videoAd.onClose(() => {
- if (isFinished) {
- //看完完整广告,可下发奖励
- close && close(true);
- SkillApi.report(1, (responseData) => {
- },(error) => {
- });
- } else {
- // 未看完广告,不下发游戏奖励
- close && close(false);
- }
- if (GameModule.audioMng) {
- GameModule.audioMng.playBgm();
- }
- });
- videoAd.show();
- }
- /// 需要在close中回调的对象
- showVideo(callBackObjct = null) {
- this.callBackObjct = callBackObjct;
- this.videoAd.load()
- .then(() => {
- if (GameModule.audioMng) {
- GameModule.audioMng.stopBgm();
- }
- this.videoAd.show();
- })
- .catch(err => {
- // if ((err.errMsg && err.errMsg.includes('fetch advertisement failed')) || (err.errCode && err.errCode == 1004)) {
- // Global.commonAlert.showCommonErrorAlert('今日份视频已经播放完啦~~');
- // } else {
- // Global.commonAlert.showCommonErrorAlert('获取视频失败');
- // }
- if (err.errMsg && err.errMsg.includes('video-ad is showed')) {
- GameGlobal.commonAlert.showCommonErrorAlert('视频正在播放请不要重复点击~~');
- return;
- } else {
- this.handelVideoErr(err);
- }
- });
- }
- handelVideoErr(res) {
- // if (isShow) {
- // Global.commonAlert.showCommonErrorAlert('获取视频失败');
- // }
- GameGlobal._adVideoState = ADVideoLoadState.fail;
- console.log('获取视频失败 fa' + res.errMsg);
- GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.fail);
- }
- // static wxPlayADVideo(adId,error,close) {
- // let videoAd = wx.createRewardedVideoAd({
- // adUnitId: adId
- // })
- // //处理获取视频错误
- // videoAd.onError( (res) => {
- // if (res.errMsg) {
- // if (res.errMsg.includes('fetch advertisement failed') || res.errCode == 1004) {
- // Global.commonAlert.showCommonErrorAlert('今日份视频已经播放完啦~~');
- // } else {
- // Global.commonAlert.showCommonErrorAlert('获取视频失败');
- // }
- // } else {
- // Global.commonAlert.showCommonErrorAlert('获取视频失败');
- // }
- // error && error();
- // });
- // //判断是否完整播放视频
- // videoAd.onClose((res) => {
- // if (res && res.isEnded || res === undefined) {
- // close && close(true);
- // } else {
- // close && close(false);
- // }
- // if (GameModule.audioMng) {
- // GameModule.audioMng.playBgm();
- // }
- // SkillApi.report(1, (responseData) => {
- // },(error) => {
- // });
- // })
- // }
- // static buildingAdId = 'adunit-4ac219410677b685'
- // static roomAdId = 'adunit-70b8fbdab9e3e7a8'
- // static offlineAdId = 'adunit-a104eef5cc68d0d3'
- // /// 抽奖广告id
- // static drawAdId = 'adunit-d067ec556ad5c9c2'
- // ///商城广告id
- // static storeAdCoinId = 'adunit-3494836fc16b0e76'
- // static storeAdDiamand = 'adunit-9d6b728b8e8a52d3'
- // static resetAdId = 'adunit-72f6285169164939'
- // static starAdId = 'adunit-0e09f5a550f077ab'
- }
- module.exports = ADVideo;
- // module.exports = ADVideoLoadState;
|