admin
2021-02-03 1981dee5aec45793d3c4ebdbc4e637528c71b3c5
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package com.weikou.beibeivideo.ui.ad;
 
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
 
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTSplashAd;
import com.qq.e.ads.cfg.VideoOption;
import com.qq.e.ads.nativ.MediaView;
import com.qq.e.ads.nativ.NativeADEventListener;
import com.qq.e.ads.nativ.NativeADMediaListener;
import com.qq.e.ads.nativ.NativeADUnifiedListener;
import com.qq.e.ads.nativ.NativeUnifiedAD;
import com.qq.e.ads.nativ.NativeUnifiedADData;
import com.qq.e.ads.nativ.VideoPreloadListener;
import com.qq.e.ads.nativ.widget.NativeAdContainer;
import com.qq.e.comm.constants.AdPatternType;
import com.qq.e.comm.util.AdError;
import com.weikou.beibeivideo.R;
import com.weikou.beibeivideo.entity.ad.PlayVideoPreADSetting;
import com.weikou.beibeivideo.util.BeibeiConstant;
import com.weikou.beibeivideo.util.DimenUtils;
import com.weikou.beibeivideo.util.ad.AdUtil;
import com.weikou.beibeivideo.util.ad.CSJConstant;
import com.weikou.beibeivideo.util.ad.GDTConstant;
import com.weikou.beibeivideo.util.ad.TTAdManagerHolder;
import com.weikou.beibeivideo.util.ui.IPageEventListener;
 
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
 
/**
 * 视频播放前贴广告
 */
public class PlayVideoPreADFragment extends Fragment implements View.OnClickListener {
 
    private final static String TAG = PlayVideoPreADFragment.class.getName();
 
    private MediaView mediaView;
 
    private NativeAdContainer mContainer;
 
    //穿山甲广告
    private FrameLayout fl_ad;
 
    private NativeUnifiedADData mGDTAdData;
 
    private TextView tv_skip;
 
    private ProgressBar pb_progress;
 
    private IPageEventListener pageEventListener;
 
    public static PlayVideoPreADFragment newInstance() {
        Bundle bundle = new Bundle();
 
        PlayVideoPreADFragment fragment = new PlayVideoPreADFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
 
    public void setPageEventListener(IPageEventListener pageEventListener) {
        this.pageEventListener = pageEventListener;
    }
 
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);
    }
 
    private ImageView img_poster;
 
    private void initView(View root) {
        mediaView = root.findViewById(R.id.gdt_media_view);
        mContainer = root.findViewById(R.id.native_ad_container);
        img_poster = root.findViewById(R.id.img_poster);
        tv_skip = root.findViewById(R.id.tv_skip);
        pb_progress = root.findViewById(R.id.pb_progress);
        fl_ad = root.findViewById(R.id.fl_ad);
    }
 
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 
 
        View root = LayoutInflater.from(getContext()).inflate(R.layout.item_video_detail_ad, null);
        initView(root);
        tv_skip.setOnClickListener(this);
        if (pageEventListener != null)
            pageEventListener.onCreateView();
        return root;
    }
 
 
    private Runnable timer = null;
 
    private synchronized void autoTimer(final View view) {
 
        if (timer == null) {
            timer = new Runnable() {
                @Override
                public void run() {
                    if (remainTime - (mGDTAdData.getVideoCurrentPosition() / 1000) > 0) {
                        tv_skip.setText((remainTime - (mGDTAdData.getVideoCurrentPosition() / 1000)) + "s后可跳过");
                        tv_skip.setClickable(false);
                    } else {
                        tv_skip.setText("跳过");
                        tv_skip.setClickable(true);
                    }
                    autoTimer(view);
                }
            };
            view.postDelayed(timer, 1000);
        } else {
            view.removeCallbacks(timer);
            view.postDelayed(timer, 1000);
        }
    }
 
 
    int remainTime = 0;
 
    Runnable playErrorRunnable = null;
 
    public void loadVideoAD(final IVideoAdListener videoAdListener) {
        loadVideoAD(videoAdListener, null);
    }
 
    public void loadVideoAD(IVideoAdListener videoAdListener, PlayVideoPreADSetting setting) {
        if (setting == null)
            setting = PlayVideoPreADSetting.PlayVideoPreADSettingFactory.createDefault();
        Log.i(TAG, "loadVideoAD");
        if (tv_skip == null)
            return;
        tv_skip.setVisibility(View.GONE);
        if (mGDTAdData != null)
            mGDTAdData.destroy();
        pb_progress.setVisibility(View.VISIBLE);
        String pid = setting.getGdtPid();
        //TODO 需要单独判断
 
        if (AdUtil.getAdType(getContext(), AdUtil.POSITION_VIDEO_PLAY_PRE) != AdUtil.AD_TYPE.gdt)
            pid = BeibeiConstant.GDT_DOWNLOAD_HINT_ID;
 
//        loadCSJ(CSJConstant.PLAY_VIDEO_PRE, videoAdListener);
        loadGDT(setting.getGdtAppId(), pid, videoAdListener);
    }
 
 
    /**
     * 加载穿山甲广告
     *
     * @param code
     * @param videoAdListener
     */
    private void loadCSJ(String code, IVideoAdListener videoAdListener) {
 
        fl_ad.setVisibility(View.VISIBLE);
        mContainer.setVisibility(View.GONE);
 
        TTAdManager ttAdManager = TTAdManagerHolder.get();
        TTAdNative mTTAdNative = ttAdManager.createAdNative(getContext());
        int width = DimenUtils.getScreenWidth(getContext());
        int height = width * 9 / 16;
 
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(code)
                .setSupportDeepLink(true)
                .setImageAcceptedSize(width, height)
                .build();
 
        mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
            @Override
            public void onError(int i, String s) {
                videoAdListener.noAd();
            }
 
            @Override
            public void onTimeout() {
                videoAdListener.noAd();
            }
 
            @Override
            public void onSplashAdLoad(TTSplashAd ad) {
                fl_ad.removeAllViews();
                fl_ad.addView(ad.getSplashView());
                ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                    @Override
                    public void onAdClicked(View view, int i) {
 
                    }
 
                    @Override
                    public void onAdShow(View view, int i) {
                        Log.i(TAG, "onAdShow");
                    }
 
                    @Override
                    public void onAdSkip() {
                        videoAdListener.finishPlay();
                    }
 
                    @Override
                    public void onAdTimeOver() {
                        videoAdListener.finishPlay();
                    }
                });
 
 
            }
        });
    }
 
    /**
     * 加载广点通广告
     *
     * @param appId
     * @param pid
     * @param videoAdListener
     */
    private void loadGDT(String appId, String pid, IVideoAdListener videoAdListener) {
        Log.i(TAG, "loadGDT");
        fl_ad.setVisibility(View.GONE);
        mContainer.setVisibility(View.VISIBLE);
        NativeUnifiedAD mAdManager = new NativeUnifiedAD(getContext(), appId, pid, new NativeADUnifiedListener() {
 
            @Override
            public void onNoAD(AdError adError) {//没有广告
                System.out.println(adError);
                pb_progress.setVisibility(View.GONE);
                if (videoAdListener != null) {
                    videoAdListener.noAd();
                }
            }
 
            @Override
            public void onADLoaded(List<NativeUnifiedADData> list) {
                Log.i(TAG, "onADLoaded");
                final NativeUnifiedADData ad = list.get(0);
                //填充内容
                mGDTAdData = ad;
                List<View> clickableViews = new ArrayList<>();
 
                ad.bindAdToView(getContext(), mContainer, null, clickableViews);
                if (ad.getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
                    Log.d("gdt_ad_mob", "视频广告");
 
                    if (playErrorRunnable == null)
                        playErrorRunnable = new Runnable() {
                            @Override
                            public void run() {
                                if (pb_progress.getVisibility() == View.VISIBLE) {
                                    Log.i(TAG, "playErrorRunnable");
                                    mGDTAdData.destroy();
                                    if (videoAdListener != null) {
                                        videoAdListener.finishPlay();
                                    }
                                }
                            }
                        };
                    //4秒内视频还没加载完成的话就跳过
                    tv_skip.removeCallbacks(playErrorRunnable);
                    tv_skip.postDelayed(playErrorRunnable, 4000);
 
                    // 如果是视频广告,可以调用preloadVideo预加载视频素材
                    ad.preloadVideo(new VideoPreloadListener() {
                        @Override
                        public void onVideoCached() {
                            Log.d(TAG, "onVideoCached");
                            //展示广告
                            VideoOption videoOption = getVideoOption();
                            ad.bindMediaView(mediaView, videoOption, new NativeADMediaListener() {
                                @Override
                                public void onVideoInit() {
                                    Log.d(TAG, "onVideoInit: ");
 
                                }
 
                                @Override
                                public void onVideoLoading() {
                                    Log.d(TAG, "onVideoLoading: ");
                                }
 
                                @Override
                                public void onVideoReady() {
                                    Log.d(TAG, "onVideoReady");
                                    img_poster.setVisibility(View.GONE);
                                    pb_progress.setVisibility(View.GONE);
                                }
 
                                @Override
                                public void onVideoLoaded(int videoDuration) {
                                    Log.d(TAG, "onVideoLoaded: ");
                                }
 
                                @Override
                                public void onVideoStart() {
                                    Log.d(TAG, "onVideoStart");
                                    if (ad.getVideoDuration() / 1000 > 5)
                                        remainTime = 5;
                                    else
                                        remainTime = ad.getVideoDuration() / 1000;
 
                                    videoAdListener.startPlay();
                                    autoTimer(img_poster);
                                    img_poster.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            tv_skip.setVisibility(View.VISIBLE);
                                        }
                                    });
 
                                }
 
                                @Override
                                public void onVideoPause() {
                                    Log.d(TAG, "onVideoPause: ");
                                }
 
                                @Override
                                public void onVideoResume() {
                                    Log.d(TAG, "onVideoResume: ");
                                }
 
                                @Override
                                public void onVideoCompleted() {
                                    Log.d(TAG, "onVideoCompleted: ");
                                    tv_skip.setVisibility(View.GONE);
                                    if (videoAdListener != null) {
                                        videoAdListener.finishPlay();
                                    }
                                }
 
                                @Override
                                public void onVideoError(AdError error) {
                                    Log.d(TAG, "onVideoError: ");
                                    if (videoAdListener != null) {
                                        videoAdListener.finishPlay();
                                    }
                                }
 
                                @Override
                                public void onVideoStop() {
                                    Log.d(TAG, "onVideoStop");
                                }
 
                                @Override
                                public void onVideoClicked() {
                                    Log.d(TAG, "onVideoClicked");
                                }
                            });
                            ad.setNativeAdEventListener(new NativeADEventListener() {
                                @Override
                                public void onADExposed() {
                                    Log.d(TAG, "onADExposed");
                                }
 
                                @Override
                                public void onADClicked() {
                                    Log.d(TAG, "onADClicked");
                                }
 
                                @Override
                                public void onADError(AdError adError) {
                                    Log.d(TAG, "onADError");
                                }
 
                                @Override
                                public void onADStatusChanged() {
                                    Log.d(TAG, "onADStatusChanged");
                                }
                            });
                            ad.startVideo();
                        }
 
                        @Override
                        public void onVideoCacheFailed(int errorNo, String msg) {
                            Log.d(TAG, "onVideoCacheFailed:" + msg);
                        }
                    });
                } else {
                    pb_progress.setVisibility(View.GONE);
                    if (videoAdListener != null) {
                        videoAdListener.noAd();
                    }
                }
            }
        });
        mAdManager.setVastClassName("com.qq.e.union.demo.adapter.vast.unified.ImaNativeDataAdapter");
        mAdManager.setVideoPlayPolicy(getVideoPlayPolicy(getContext()));
        mAdManager.setVideoADContainerRender(VideoOption.VideoADContainerRender.SDK);
        mAdManager.loadData(1);
 
        tv_skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mGDTAdData != null)
                    mGDTAdData.stopVideo();
                if (videoAdListener != null)
                    videoAdListener.finishPlay();
                tv_skip.setVisibility(View.GONE);
            }
        });
    }
 
 
    @Nullable
    public static VideoOption getVideoOption() {
        VideoOption.Builder builder = new VideoOption.Builder();
        builder.setAutoPlayPolicy(VideoOption.AutoPlayPolicy.NEVER);
        builder.setAutoPlayMuted(false);
        builder.setDetailPageMuted(false);
        builder.setNeedCoverImage(true);
        builder.setNeedProgressBar(false);
        builder.setEnableDetailPage(true);
        builder.setEnableUserControl(false);
        return builder.build();
    }
 
 
    public static int getVideoPlayPolicy(Context context) {
 
        if (1 > 0)
            return VideoOption.VideoPlayPolicy.MANUAL;
 
        VideoOption option = getVideoOption();
        if (option == null) {
            return VideoOption.VideoPlayPolicy.AUTO;
        }
 
        int autoPlayPolicy = option.getAutoPlayPolicy();
 
        if (autoPlayPolicy == VideoOption.AutoPlayPolicy.ALWAYS) {
            return VideoOption.VideoPlayPolicy.AUTO;
        } else if (autoPlayPolicy == VideoOption.AutoPlayPolicy.WIFI) {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo wifiNetworkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            return wifiNetworkInfo != null && wifiNetworkInfo.isConnected() ? VideoOption.VideoPlayPolicy.AUTO
                    : VideoOption.VideoPlayPolicy.MANUAL;
        } else if (autoPlayPolicy == VideoOption.AutoPlayPolicy.NEVER) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
 
            // 简单的播放策略示例,虽然VideoOption设置了从不自动播放,SDK不会主动触发自动播放
            // 但晚上10点以后,开发者自行调用了startVideoPlay进行了视频播放
            // 这在用户看来仍然是"自动播放"的,因为用户没有进行任何操作,视频就开始播放了
            return hour >= 22 ? VideoOption.VideoPlayPolicy.AUTO : VideoOption.VideoPlayPolicy.MANUAL;
        }
 
        return VideoOption.VideoPlayPolicy.UNKNOWN;
    }
 
 
    @Override
    public void onResume() {
        Log.i(TAG, "onResume");
        super.onResume();
        if (mGDTAdData != null) {
            // 必须要在Activity.onResume()时通知到广告数据,以便重置广告恢复状态
            mGDTAdData.resume();
        }
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mGDTAdData != null) {
            // 必须要在Actiivty.destroy()时通知到广告数据,以便释放内存
            mGDTAdData.destroy();
        }
        if (img_poster != null && timer != null)
            img_poster.removeCallbacks(timer);
    }
 
    @Override
    public void onClick(View v) {
 
        switch (v.getId()) {
            case R.id.tv_skip:
 
 
                break;
        }
 
    }
 
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }
 
    public interface IVideoAdListener {
 
        //结束播放
        public void finishPlay();
 
        //没有广告填充
        public void noAd();
 
        //开始播放
        public void startPlay();
 
    }
 
 
}