ADVideo.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. const GameModule = require("../utils/GameModule");
  2. const SkillApi = require('../net/SkillApi');
  3. // const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  4. const {GameNotificationKey, ADVideoLoadState} = require('../utils/GameEnum');
  5. class ADVideo {
  6. // constructor() {
  7. // this.isNeedShowErr = false;
  8. // }
  9. wxCreateADVideo() {
  10. let defualtADdId = 'adunit-9d6b728b8e8a52d3';
  11. let videoAd = wx.createRewardedVideoAd({
  12. adUnitId: defualtADdId
  13. });
  14. /// 默认是加载成功的 啦
  15. GameGlobal._adVideoState = ADVideoLoadState.success;
  16. this.videoAd = videoAd;
  17. videoAd.load()
  18. .then(() => {
  19. this.isFirstLoad = false;
  20. GameGlobal._adVideoState = ADVideoLoadState.success;
  21. GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.success);
  22. console.log('视频加载成功');
  23. })
  24. .catch(err => {
  25. // if ((err.errMsg && err.errMsg.includes('fetch advertisement failed')) || (err.errCode && err.errCode == 1004)) {
  26. // Global.commonAlert.showCommonErrorAlert('今日份视频已经播放完啦~~');
  27. // } else {
  28. // Global.commonAlert.showCommonErrorAlert('获取视频失败');
  29. // }
  30. this.handelVideoErr(err);
  31. this.isFirstLoad = false;
  32. });
  33. // videoAd.onLoad(() => {
  34. // /// 视频加载成功
  35. // Global._adVideoState = ADVideoLoadState.success;
  36. // GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.success);
  37. // console.log('视频加载成功');
  38. // });
  39. // 处理获取视频错误
  40. videoAd.onError( (res) => {
  41. this.handelVideoErr(res);
  42. });
  43. videoAd.onClose((res) => {
  44. if (res && res.isEnded || res === undefined) {
  45. console.log('视频正常关闭');
  46. GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.closeFinish, this.callBackObjct);
  47. } else {
  48. console.log('视频未正常关闭');
  49. GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.closeFail, this.callBackObjct);
  50. }
  51. if (GameModule.audioMng) {
  52. GameModule.audioMng.playBgm();
  53. }
  54. SkillApi.report(1, (responseData) => {
  55. },(error) => {
  56. });
  57. });
  58. }
  59. static qqPlayADVideo(error,close) {
  60. var isFinished = false;
  61. var videoAd = BK.Advertisement.createVideoAd();
  62. videoAd.onLoad(() => {
  63. //加载成功
  64. BK.Script.log(1,1,"onLoad")
  65. });
  66. videoAd.onPlayStart(() => {
  67. if (GameModule.audioMng) {
  68. GameModule.audioMng.stopBgm();
  69. }
  70. //开始播放
  71. BK.Script.log(1,1,"onPlayStart")
  72. });
  73. videoAd.onPlayFinish(() => {
  74. //播放结束
  75. BK.Script.log(1,1,"onPlayFinish")
  76. isFinished = true;
  77. });
  78. videoAd.onError((err) => {
  79. //加载失败
  80. BK.Script.log(1,1,"onError code:"+err.code+" msg:"+err.msg);
  81. GameGlobal.commonAlert.showCommonErrorAlert('获取视频失败');
  82. error && error();
  83. });
  84. videoAd.onClose(() => {
  85. if (isFinished) {
  86. //看完完整广告,可下发奖励
  87. close && close(true);
  88. SkillApi.report(1, (responseData) => {
  89. },(error) => {
  90. });
  91. } else {
  92. // 未看完广告,不下发游戏奖励
  93. close && close(false);
  94. }
  95. if (GameModule.audioMng) {
  96. GameModule.audioMng.playBgm();
  97. }
  98. });
  99. videoAd.show();
  100. }
  101. /// 需要在close中回调的对象
  102. showVideo(callBackObjct = null) {
  103. this.callBackObjct = callBackObjct;
  104. this.videoAd.load()
  105. .then(() => {
  106. if (GameModule.audioMng) {
  107. GameModule.audioMng.stopBgm();
  108. }
  109. this.videoAd.show();
  110. })
  111. .catch(err => {
  112. // if ((err.errMsg && err.errMsg.includes('fetch advertisement failed')) || (err.errCode && err.errCode == 1004)) {
  113. // Global.commonAlert.showCommonErrorAlert('今日份视频已经播放完啦~~');
  114. // } else {
  115. // Global.commonAlert.showCommonErrorAlert('获取视频失败');
  116. // }
  117. if (err.errMsg && err.errMsg.includes('video-ad is showed')) {
  118. GameGlobal.commonAlert.showCommonErrorAlert('视频正在播放请不要重复点击~~');
  119. return;
  120. } else {
  121. this.handelVideoErr(err);
  122. }
  123. });
  124. }
  125. handelVideoErr(res) {
  126. // if (isShow) {
  127. // Global.commonAlert.showCommonErrorAlert('获取视频失败');
  128. // }
  129. GameGlobal._adVideoState = ADVideoLoadState.fail;
  130. console.log('获取视频失败 fa' + res.errMsg);
  131. GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.fail);
  132. }
  133. // static wxPlayADVideo(adId,error,close) {
  134. // let videoAd = wx.createRewardedVideoAd({
  135. // adUnitId: adId
  136. // })
  137. // //处理获取视频错误
  138. // videoAd.onError( (res) => {
  139. // if (res.errMsg) {
  140. // if (res.errMsg.includes('fetch advertisement failed') || res.errCode == 1004) {
  141. // Global.commonAlert.showCommonErrorAlert('今日份视频已经播放完啦~~');
  142. // } else {
  143. // Global.commonAlert.showCommonErrorAlert('获取视频失败');
  144. // }
  145. // } else {
  146. // Global.commonAlert.showCommonErrorAlert('获取视频失败');
  147. // }
  148. // error && error();
  149. // });
  150. // //判断是否完整播放视频
  151. // videoAd.onClose((res) => {
  152. // if (res && res.isEnded || res === undefined) {
  153. // close && close(true);
  154. // } else {
  155. // close && close(false);
  156. // }
  157. // if (GameModule.audioMng) {
  158. // GameModule.audioMng.playBgm();
  159. // }
  160. // SkillApi.report(1, (responseData) => {
  161. // },(error) => {
  162. // });
  163. // })
  164. // }
  165. // static buildingAdId = 'adunit-4ac219410677b685'
  166. // static roomAdId = 'adunit-70b8fbdab9e3e7a8'
  167. // static offlineAdId = 'adunit-a104eef5cc68d0d3'
  168. // /// 抽奖广告id
  169. // static drawAdId = 'adunit-d067ec556ad5c9c2'
  170. // ///商城广告id
  171. // static storeAdCoinId = 'adunit-3494836fc16b0e76'
  172. // static storeAdDiamand = 'adunit-9d6b728b8e8a52d3'
  173. // static resetAdId = 'adunit-72f6285169164939'
  174. // static starAdId = 'adunit-0e09f5a550f077ab'
  175. }
  176. module.exports = ADVideo;
  177. // module.exports = ADVideoLoadState;