admin
2022-07-25 3a01c2df2522acec74565b343a1dbb7294667a55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.weikou.beibeivideo.util.ad;
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
 
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.qq.e.ads.nativ.NativeExpressADView;
import com.ut.device.UTDevice;
import com.weikou.beibeivideo.entity.ad.ExpressAdContainer;
import com.weikou.beibeivideo.entity.ad.RewardVideoAdContainer;
 
import java.util.List;
 
/**
 * 激励视频广告管理
 */
public class RewardVideoAdManager {
 
 
    final static String TAG = RewardVideoAdManager.class.getSimpleName();
 
    AdUtil.AD_TYPE sourceType = AdUtil.AD_TYPE.gdt;
 
    private TTAdNative mTTAdNative;
    private Context mContext;
 
 
    public RewardVideoAdManager(AdUtil.AD_TYPE adSource, Context context) {
        sourceType = adSource;
        this.mContext = context;
        if (sourceType == AdUtil.AD_TYPE.csj) {
            TTAdManager ttAdManager = null;
            try {
                ttAdManager = TTAdManagerHolder.get();
                mTTAdNative = ttAdManager.createAdNative(context);
            } catch (Exception e) {
                e.printStackTrace();
            }
 
        }
    }
 
 
    //加载小广告
 
    /**
     * 加载广告
     *
     * @param widthDP
     * @param heightDP
     * @param hrizontal
     * @param adLoadListener
     */
    public void loadAd(int widthDP, int heightDP, String pid, boolean hrizontal, final IAdLoadListener adLoadListener, IAdShowListener showListener) {
        if (sourceType == AdUtil.AD_TYPE.csj) {
            loadCSJ(pid, widthDP, heightDP, hrizontal, new ICSJAdLoadListener() {
                @Override
                public void onSuccess(TTRewardVideoAd ad) {
                    adLoadListener.onSuccess(new RewardVideoAdContainer(ad));
                }
 
                @Override
                public void onError(String msg) {
                    adLoadListener.onError(msg);
                }
            });
        }
    }
 
 
    /**
     * 穿山甲广告
     *
     * @param codeId
     * @param width
     * @param height
     * @param count
     * @param adLoadListener
     */
 
    private TTRewardVideoAd mTTRewardVideoAd;
 
    private void loadCSJ(String codeId, int width, int height, boolean hrizontal, final ICSJAdLoadListener adLoadListener) {
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(codeId)
                .setSupportDeepLink(true)
                .setUserID(UTDevice.getUtdid(mContext))
                .setExpressViewAcceptedSize(width, height)
                .setOrientation(hrizontal ? TTAdConstant.HORIZONTAL : TTAdConstant.VERTICAL)
                .build();
        if (mTTAdNative == null) {
            if (adLoadListener != null)
                adLoadListener.onError("mTTAdNative为空");
            return;
        }
        mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            //请求广告失败
            @Override
            public void onError(int code, String message) {
                adLoadListener.onError(code + ":" + message);
            }
 
            //视频广告加载后,视频资源缓存到本地的回调,在此回调后,播放本地视频,流畅不阻塞。
            @Override
            public void onRewardVideoCached() {
                adLoadListener.onSuccess(mTTRewardVideoAd);
            }
 
            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
                adLoadListener.onSuccess(mTTRewardVideoAd);
            }
 
            //视频广告的素材加载完毕,比如视频url等,在此回调后,可以播放在线视频,网络不好可能出现加载缓冲,影响体验。
            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ad) {
                mTTRewardVideoAd = ad;
            }
        });
    }
 
    /**
     * 渲染模板和填充广告
     *
     * @param activity
     * @param ad
     * @param showListener
     */
    public static void showAd(Activity activity, final RewardVideoAdContainer ad, RewardVideoAdManager.IAdShowListener showListener) {
        if (activity == null)
            return;
 
        if (ad == null) {//广告为空
            return;
        }
        if (ad.getCsj() != null) {
            ad.getCsj().setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
                //广告展示回调
                @Override
                public void onAdShow() {
                    Log.i(TAG, "onAdShow");
                    showListener.onShow();
                }
 
                //下载回调
                @Override
                public void onAdVideoBarClick() {
                    Log.i(TAG, "onAdVideoBarClick");
                }
 
                //广告关闭
                @Override
                public void onAdClose() {
                    Log.i(TAG, "onAdClose");
                    showListener.onClose();
                }
 
                //视频播放完成回调
                @Override
                public void onVideoComplete() {
                    Log.i(TAG, "onVideoComplete");
                }
 
                @Override
                public void onVideoError() {
                    Log.i(TAG, "onVideoError");
                    showListener.onError("onVideoError");
                }
 
                //奖励验证回调
                //rewardVerify:是否有效,rewardAmount:奖励梳理,rewardName:奖励名称,code:错误码,msg:错误信息
                @Override
                public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName, int code, String msg) {
                    Log.i(TAG, "onRewardVerify");
                    Log.i(TAG, "rewardVerify:" + rewardVerify);
                    Log.i(TAG, "rewardAmount:" + rewardAmount);
                    Log.i(TAG, "rewardName:" + rewardName);
                    Log.i(TAG, "code:" + code);
                    Log.i(TAG, "msg:" + msg);
 
                    if (rewardVerify) {
                        showListener.onReward();
                    }
                }
 
                @Override
                public void onRewardArrived(boolean b, int i, Bundle bundle) {
 
                }
 
                //跳过视频播放回调
                @Override
                public void onSkippedVideo() {
                    Log.i(TAG, "onSkippedVideo");
                }
            });
            ad.getCsj().showRewardVideoAd(activity, TTAdConstant.RitScenes.CUSTOMIZE_SCENES, "scenes_test");
            ad.setCsj(null);
        }
 
    }
 
 
    interface IGDTAdLoadListener {
        public void onSuccess(List<NativeExpressADView> adList);
    }
 
    interface ICSJAdLoadListener {
        public void onSuccess(TTRewardVideoAd ad);
 
        public void onError(String msg);
    }
 
 
    public interface IAdLoadListener {
        public void onSuccess(RewardVideoAdContainer ad);
 
        public void onError(String msg);
    }
 
    public interface IAdShowListener {
        //展示
        public void onShow();
 
        //奖励发放
        public void onReward();
 
        public void onError(String msg);
 
        public void onClose();
    }
 
    public interface IAdEventListener {
        public void closeAd(ExpressAdContainer ad);
    }
 
 
}