当前位置:   article > 正文

Android 接入穿山甲SDK之激励视频广告_安卓如何接入sdk穿山甲

安卓如何接入sdk穿山甲

大家可以先参考我的上一篇博客 Banner广告 传送门

首先创建一个脚本具体内容如下

  1. package com.unity3d.player.chuanshanjia;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import androidx.annotation.Nullable;
  7. import com.bytedance.sdk.openadsdk.AdSlot;
  8. import com.bytedance.sdk.openadsdk.TTAdConstant;
  9. import com.bytedance.sdk.openadsdk.TTAdLoadType;
  10. import com.bytedance.sdk.openadsdk.TTAdManager;
  11. import com.bytedance.sdk.openadsdk.TTAdNative;
  12. import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
  13. import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
  14. import com.unity3d.player.R;
  15. import com.unity3d.player.UnityPlayer;
  16. import com.unity3d.player.UnityPlayerActivity;
  17. import com.unity3d.player.tools.TToast;
  18. public class RewardVideoActivity extends Activity{
  19. private static RewardVideoActivity _Instance;
  20. public static RewardVideoActivity Inst(){
  21. return _Instance;
  22. }
  23. private TTAdNative mTTAdNative;
  24. private boolean isExpress = false;
  25. private String TAG = "RewardVideoActivity";
  26. private boolean mIsLoaded = false; //视频是否加载完成
  27. private TTRewardVideoAd mttRewardVideoAd;
  28. // 是否开放进阶奖励功能
  29. private boolean isEnableAdvancedReward = false;
  30. private boolean mHasShowDownloadActive = false;
  31. @Override
  32. protected void onCreate(@Nullable Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  35. _Instance = this;
  36. //step1:初始化sdk
  37. TTAdManager ttAdManager = TTAdManagerHolder.get();
  38. //step2:(可选,强烈建议在合适的时机调用):申请部分权限,如read_phone_state,防止获取不了imei时候,下载类广告没有填充的问题。
  39. TTAdManagerHolder.get().requestPermissionIfNecessary(this);
  40. //step3:创建TTAdNative对象,用于调用广告请求接口
  41. mTTAdNative = ttAdManager.createAdNative(getApplicationContext());
  42. Intent it = getIntent();
  43. String codeId = it.getStringExtra("codeId");
  44. int orientation = it.getIntExtra("orientation",2);
  45. String userId = it.getStringExtra("userId");
  46. AddLoad(codeId,orientation,userId);
  47. }
  48. //codeId 平台创建的代码位ID 以9开头9位数字 orientation 期望视频的播放方向 1竖屏 2横屏
  49. public void AddLoad(final String codeId, int orientation,String userId){
  50. AdSlot adSlot;
  51. adSlot = new AdSlot.Builder()
  52. .setCodeId(codeId)
  53. //模板广告需要设置期望个性化模板广告的大小,单位dp,激励视频场景,只要设置的值大于0即可
  54. //且仅是模板渲染的代码位ID使用,非模板渲染代码位切勿使用
  55. .setExpressViewAcceptedSize(1080,1920)
  56. .setUserID(userId)//tag_id
  57. .setMediaExtra("media_extra") //附加参数
  58. .setOrientation(orientation) //必填参数,期望视频的播放方向:TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
  59. .setAdLoadType(TTAdLoadType.LOAD)//推荐使用,用于标注此次的广告请求用途为预加载(当做缓存)还是实时加载,方便后续为开发者优化相关策略
  60. .build();
  61. mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
  62. @Override
  63. public void onError(int code, String message) {
  64. Log.e(TAG, "Callback --> onError: " + code + ", " + String.valueOf(message));
  65. goToMainActivity();
  66. TToast.show(RewardVideoActivity.this, message);
  67. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsLoadFailed);
  68. }
  69. //视频广告加载后,视频资源缓存到本地的回调,在此回调后,播放本地视频,流畅不阻塞。
  70. @Override
  71. public void onRewardVideoCached() {
  72. Log.e(TAG, "Callback --> onRewardVideoCached");
  73. mIsLoaded = true;
  74. TToast.show(RewardVideoActivity.this, "Callback --> rewardVideoAd video cached");
  75. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsCached);
  76. }
  77. @Override
  78. public void onRewardVideoCached(TTRewardVideoAd ad) {
  79. Log.e(TAG, "Callback --> onRewardVideoCached");
  80. mIsLoaded = true;
  81. TToast.show(RewardVideoActivity.this, "Callback --> rewardVideoAd video cached");
  82. ad.showRewardVideoAd(RewardVideoActivity.this, TTAdConstant.RitScenes.CUSTOMIZE_SCENES, "scenes_test");
  83. }
  84. //视频广告的素材加载完毕,比如视频url等,在此回调后,可以播放在线视频,网络不好可能出现加载缓冲,影响体验。
  85. @Override
  86. public void onRewardVideoAdLoad(TTRewardVideoAd ad) {
  87. Log.e(TAG, "Callback --> onRewardVideoAdLoad");
  88. TToast.show(RewardVideoActivity.this, "rewardVideoAd loaded 广告类型:" + getAdType(ad.getRewardVideoAdType()));
  89. mIsLoaded = false;
  90. mttRewardVideoAd = ad;
  91. mttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
  92. @Override
  93. //广告的展示回调
  94. public void onAdShow() {
  95. Log.d(TAG, "Callback --> rewardVideoAd show");
  96. TToast.show(RewardVideoActivity.this, "Callback --> rewardVideoAd show");
  97. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsShowSucceed);
  98. }
  99. @Override
  100. //广告下载bar点击回调
  101. public void onAdVideoBarClick() {
  102. Log.d(TAG, "Callback --> rewardVideoAd bar click");
  103. TToast.show(RewardVideoActivity.this, "Callback --> onAdVideoBarClick");
  104. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsClick);
  105. }
  106. @Override
  107. //广告关闭回调
  108. public void onAdClose() {
  109. Log.d(TAG, "Callback --> rewardVideoAd close");
  110. if (isEnableAdvancedReward) {
  111. Log.d(TAG, "本次奖励共发放:");
  112. }
  113. TToast.show(RewardVideoActivity.this, "Callback --> rewardVideoAd close");
  114. goToMainActivity();
  115. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsClosed);
  116. }
  117. //视频播放完成回调
  118. @Override
  119. public void onVideoComplete() {
  120. Log.d(TAG, "Callback --> rewardVideoAd complete");
  121. TToast.show(RewardVideoActivity.this, "Callback --> rewardVideoAd complete");
  122. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsPlayedFinished);
  123. }
  124. @Override
  125. public void onVideoError() {
  126. Log.e(TAG, "Callback --> rewardVideoAd error");
  127. TToast.show(RewardVideoActivity.this, "Callback --> rewardVideoAd error");
  128. goToMainActivity();
  129. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsPlayeFailed);
  130. }
  131. //视频播放完成后,奖励验证回调,rewardVerify:是否有效,rewardAmount:奖励梳理,rewardName:奖励名称
  132. @Override
  133. public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName, int errorCode, String errorMsg) {
  134. String logString = "verify:" + rewardVerify + " amount:" + rewardAmount +
  135. " name:" + rewardName + " errorCode:" + errorCode + " errorMsg:" + errorMsg;
  136. Log.e(TAG, "onRewardVerify Callback --> " + logString);
  137. // goToMainActivity("");
  138. TToast.show(RewardVideoActivity.this, "Callback --> onRewardVerify");
  139. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsRewardVerify);
  140. }
  141. /**
  142. * 激励视频播放完毕,验证是否有效发放奖励的回调 4400版本新增
  143. *
  144. * @param isRewardValid 奖励有效
  145. * @param rewardType 奖励类型,0:基础奖励 >0:进阶奖励
  146. * @param extraInfo 奖励的额外参数
  147. */
  148. @Override
  149. public void onRewardArrived(boolean isRewardValid, int rewardType, Bundle extraInfo) {
  150. Log.e(TAG, "Callback --> rewardVideoAd has onRewardArrived ");
  151. TToast.show(RewardVideoActivity.this, "Callback --> onRewardArrived");
  152. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsRewardArrived);
  153. }
  154. //跳过视频播放回调
  155. @Override
  156. public void onSkippedVideo() {
  157. Log.e(TAG, "Callback --> rewardVideoAd has onSkippedVideo");
  158. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsSkip);
  159. }
  160. });
  161. mttRewardVideoAd.setRewardPlayAgainInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
  162. @Override
  163. public void onAdShow() {
  164. Log.d(TAG, "Callback --> rewardPlayAgain show");
  165. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsShowSucceed);
  166. }
  167. @Override
  168. public void onAdVideoBarClick() {
  169. Log.d(TAG, "Callback --> rewardPlayAgain bar click");
  170. TToast.show(RewardVideoActivity.this, "Callback --> rewardPlayAgain bar click");
  171. }
  172. @Override
  173. public void onAdClose() {
  174. Log.d(TAG, "Callback --> rewardPlayAgain close");
  175. TToast.show(RewardVideoActivity.this, "Callback --> rewardPlayAgain bar click");
  176. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsClosed);
  177. }
  178. //视频播放完成回调
  179. @Override
  180. public void onVideoComplete() {
  181. Log.d(TAG, "Callback --> rewardPlayAgain complete");
  182. TToast.show(RewardVideoActivity.this, "Callback --> rewardPlayAgain complete");
  183. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsPlayedFinished);
  184. }
  185. @Override
  186. public void onVideoError() {
  187. Log.e(TAG, "Callback --> rewardPlayAgain error");
  188. TToast.show(RewardVideoActivity.this, "Callback --> rewardPlayAgain error");
  189. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsPlayeFailed);
  190. }
  191. //视频播放完成后,奖励验证回调,rewardVerify:是否有效,rewardAmount:奖励梳理,rewardName:奖励名称
  192. @Override
  193. public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName, int errorCode, String errorMsg) {
  194. String logString = "rewardPlayAgain verify:" + rewardVerify + " amount:" + rewardAmount +
  195. " name:" + rewardName + " errorCode:" + errorCode + " errorMsg:" + errorMsg;
  196. Log.e(TAG, "Callback --> " + logString);
  197. TToast.show(RewardVideoActivity.this, "Callback --> rewardPlayAgain onRewardVerify");
  198. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsRewardVerify);
  199. }
  200. @Override
  201. public void onRewardArrived(boolean isRewardValid, int rewardType, Bundle extraInfo) {
  202. Log.e(TAG, "Callback --> rewardPlayAgain has onRewardArrived ");
  203. TToast.show(RewardVideoActivity.this, "Callback --> rewardPlayAgain onRewardArrived");
  204. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsRewardVerify);
  205. }
  206. @Override
  207. public void onSkippedVideo() {
  208. Log.e(TAG, "Callback --> rewardPlayAgain has onSkippedVideo");
  209. TToast.show(RewardVideoActivity.this, "Callback --> rewardPlayAgain onSkippedVideo");
  210. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsSkip);
  211. }
  212. });
  213. mttRewardVideoAd.setDownloadListener(new TTAppDownloadListener() {
  214. @Override
  215. public void onIdle() {
  216. mHasShowDownloadActive = false;
  217. }
  218. @Override
  219. public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
  220. Log.d("DML", "onDownloadActive==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);
  221. if (!mHasShowDownloadActive) {
  222. mHasShowDownloadActive = true;
  223. Log.d("DML", "onDownloadActive==totalBytes= DownLoad ......." );
  224. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsDownLoading);
  225. }
  226. }
  227. @Override
  228. public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
  229. Log.d("DML", "onDownloadPaused===totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);
  230. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsDownLoadPaused);
  231. }
  232. @Override
  233. public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
  234. Log.d("DML", "onDownloadFailed==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);
  235. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsDownLoadFailed);
  236. }
  237. @Override
  238. public void onDownloadFinished(long totalBytes, String fileName, String appName) {
  239. Log.d("DML", "onDownloadFinished==totalBytes=" + totalBytes + ",fileName=" + fileName + ",appName=" + appName);
  240. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsDownLoadSucceed);
  241. }
  242. @Override
  243. public void onInstalled(String fileName, String appName) {
  244. Log.d("DML", "onInstalled==" + ",fileName=" + fileName + ",appName=" + appName);
  245. UnityPlayer.UnitySendMessage("Sdk","RewardVideoCallBack",CSJAdsStatusType.csjAdsStartInstalle);
  246. }
  247. });
  248. }
  249. });
  250. }
  251. public void SetExpress(boolean exp){
  252. isExpress = exp;
  253. }
  254. private void goToMainActivity(){
  255. Intent intent=new Intent(RewardVideoActivity.this, UnityPlayerActivity.class);
  256. startActivity(intent);
  257. this.finish();
  258. }
  259. private String getAdType(int type) {
  260. switch (type) {
  261. case TTAdConstant.AD_TYPE_COMMON_VIDEO:
  262. return "普通激励视频,type=" + type;
  263. case TTAdConstant.AD_TYPE_PLAYABLE_VIDEO:
  264. return "Playable激励视频,type=" + type;
  265. case TTAdConstant.AD_TYPE_PLAYABLE:
  266. return "纯Playable,type=" + type;
  267. case TTAdConstant.AD_TYPE_LIVE:
  268. return "直播流,type=" + type;
  269. }
  270. return "未知类型+type=" + type;
  271. }
  272. }

脚本创建完毕之后在AndroidMainfest文件中加入如下代码

  1. <activity
  2. android:name="com.unity3d.player.chuanshanjia.RewardVideoActivity"
  3. android:configChanges="keyboard|orientation|screenSize"
  4. android:screenOrientation="fullSensor" />

 使用方式

  1. public void RewardVideo(String codeId, int orientation,String userId){
  2. Log.d(TAG, "RewardVideo codeId:"+codeId+"orientation"+orientation+"userId"+userId);
  3. Intent intent=new Intent(UnityPlayerActivity.this, RewardVideoActivity.class);
  4. intent.putExtra("codeId",codeId);
  5. intent.putExtra("orientation",orientation);
  6. intent.putExtra("userId",userId);
  7. startActivity(intent);
  8. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/322999
推荐阅读
相关标签
  

闽ICP备14008679号