admin
2020-08-14 b05064ad88c7c65bd534292f30807ad2eff25a26
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
package com.weikou.beibeivideo.ui.ad;
 
import android.content.Context;
import android.graphics.Bitmap;
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.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
 
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
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.yeshi.buwanshequ.R;
 
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
 
public class VideoDetailVideoAdFragment extends Fragment implements View.OnClickListener {
 
    private MediaView mediaView;
 
    private NativeAdContainer mContainer;
 
    private NativeUnifiedADData mAdData;
 
    private TextView tv_skip;
 
    private ProgressBar pb_progress;
 
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
 
    private ImageView iv_logo, img_poster;
    private TextView text_title;
    private TextView text_desc;
    private Button mCTAButton, mDownloadButton;
 
    private void initView(View root) {
        mediaView = root.findViewById(R.id.gdt_media_view);
        mContainer = root.findViewById(R.id.native_ad_container);
        iv_logo = root.findViewById(R.id.img_logo);
        img_poster = root.findViewById(R.id.img_poster);
        text_title = root.findViewById(R.id.text_title);
        text_desc = root.findViewById(R.id.text_desc);
        mCTAButton = root.findViewById(R.id.btn_cta);
        mDownloadButton = root.findViewById(R.id.btn_download);
        tv_skip = root.findViewById(R.id.tv_skip);
        pb_progress = root.findViewById(R.id.pb_progress);
    }
 
    @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);
//        loadVideoAD();
        tv_skip.setOnClickListener(this);
        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 - (mAdData.getVideoCurrentPosition() / 1000) > 0) {
                        tv_skip.setText((remainTime - (mAdData.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;
 
    public void loadVideoAD(final IVideoAdListener videoAdListener) {
        tv_skip.setVisibility(View.GONE);
        if (mAdData != null)
            mAdData.destroy();
        NativeUnifiedAD mAdManager = new NativeUnifiedAD(getContext(), "5061928359494942", new NativeADUnifiedListener() {
 
            @Override
            public void onNoAD(AdError adError) {
                System.out.println(adError);
            }
 
            @Override
            public void onADLoaded(List<NativeUnifiedADData> list) {
                System.out.println(list);
                final NativeUnifiedADData ad = list.get(0);
                //填充内容
                mAdData = ad;
                renderAdUi(ad);
                List<View> clickableViews = new ArrayList<>();
                clickableViews.add(mDownloadButton);
 
                ad.bindAdToView(getContext(), mContainer, null, clickableViews);
                if (ad.getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
                    final String TAG = "NativeUnifiedADData";
                    // 如果是视频广告,可以调用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: ");
                                    pb_progress.setVisibility(View.VISIBLE);
                                }
 
                                @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 > 10)
                                        remainTime = 10;
                                    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");
                                    updateAdAction(mDownloadButton, ad);
                                }
                            });
 
                            updateAdAction(mDownloadButton, ad);
 
                            List<View> CTAViews = new ArrayList<>();
                            CTAViews.add(mCTAButton);
                            ad.bindCTAViews(CTAViews);
                            String ctaText = ad.getCTAText(); //获取组件文案
                            if (!TextUtils.isEmpty(ctaText)) {
                                //如果拉取广告包含CTA组件,则渲染该组件
                                //当广告中有营销组件时,隐藏下载按钮,仅为demo示例所用,开发者可自行决定mDownloadButton按钮是否显示
                                mCTAButton.setText(ctaText);
                                mCTAButton.setVisibility(View.VISIBLE);
                                mDownloadButton.setVisibility(View.INVISIBLE);
                            }
                            ad.startVideo();
                        }
 
                        @Override
                        public void onVideoCacheFailed(int errorNo, String msg) {
                            Log.d(TAG, "onVideoCacheFailed:" + msg);
                        }
                    });
                }
            }
        });
        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 (mAdData != null)
                    mAdData.stopVideo();
                if (videoAdListener != null)
                    videoAdListener.finishPlay();
                tv_skip.setVisibility(View.GONE);
            }
        });
 
    }
 
    public static void updateAdAction(Button button, NativeUnifiedADData ad) {
        if (!ad.isAppAd()) {
            button.setText("浏览");
            return;
        }
        switch (ad.getAppStatus()) {
            case 0:
                button.setText("下载");
                break;
            case 1:
                button.setText("启动");
                break;
            case 2:
                button.setText("更新");
                break;
            case 4:
                button.setText(ad.getProgress() + "%");
                break;
            case 8:
                button.setText("安装");
                break;
            case 16:
                button.setText("下载失败,重新下载");
                break;
            default:
                button.setText("浏览");
                break;
        }
    }
 
 
    private void renderAdUi(final NativeUnifiedADData ad) {
        int patternType = ad.getAdPatternType();
        if (patternType == AdPatternType.NATIVE_VIDEO) {
            Glide.with(getContext()).load(ad.getIconUrl()).into(iv_logo);
            Glide.with(getContext()).load(ad.getImgUrl())
                    .asBitmap().into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    if (img_poster.getVisibility() == View.VISIBLE) {
                        img_poster.setImageBitmap(resource);
                    }
                }
            });
            text_title.setText(ad.getTitle());
            text_desc.setText(ad.getDesc());
        }
    }
 
    @Nullable
    public static VideoOption getVideoOption() {
        VideoOption.Builder builder = new VideoOption.Builder();
        builder.setAutoPlayPolicy(VideoOption.AutoPlayPolicy.NEVER);
        builder.setAutoPlayMuted(true);
        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() {
        super.onResume();
        if (mAdData != null) {
            // 必须要在Activity.onResume()时通知到广告数据,以便重置广告恢复状态
            mAdData.resume();
        }
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mAdData != null) {
            // 必须要在Actiivty.destroy()时通知到广告数据,以便释放内存
            mAdData.destroy();
        }
    }
 
    @Override
    public void onClick(View v) {
 
        switch (v.getId()) {
            case R.id.tv_skip:
 
 
                break;
        }
 
    }
 
 
    public interface IVideoAdListener {
 
        //结束播放
        public void finishPlay();
 
        //开始播放
        public void startPlay();
 
    }
}