ListVideoUtil.java 18.0 KB
Newer Older
S
shuyu 已提交
1 2
package com.shuyu.gsyvideoplayer.utils;

3
import android.app.Activity;
S
shuyu 已提交
4
import android.content.Context;
5
import android.graphics.Color;
S
shuyu 已提交
6
import android.graphics.Point;
7
import android.os.Handler;
S
1.1.6  
shuyu 已提交
8
import android.view.Gravity;
S
shuyu 已提交
9 10
import android.view.View;
import android.view.ViewGroup;
S
1.1.6  
shuyu 已提交
11
import android.widget.FrameLayout;
S
shuyu 已提交
12

13
import com.shuyu.gsyvideoplayer.GSYVideoPlayer;
14
import com.shuyu.gsyvideoplayer.R;
S
1.1.9  
shuyu 已提交
15
import com.shuyu.gsyvideoplayer.listener.StandardVideoAllCallBack;
S
1.1.6  
shuyu 已提交
16
import com.shuyu.gsyvideoplayer.video.GSYBaseVideoPlayer;
S
shuyu 已提交
17
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
S
1.2.4  
shuyu 已提交
18
import com.transitionseverywhere.TransitionManager;
S
shuyu 已提交
19

S
1.3.0  
shuyu 已提交
20 21 22
import java.io.File;
import java.util.Map;

S
1.1.6  
shuyu 已提交
23 24
import static com.shuyu.gsyvideoplayer.utils.CommonUtil.getActionBarHeight;
import static com.shuyu.gsyvideoplayer.utils.CommonUtil.getStatusBarHeight;
S
shuyu 已提交
25 26
import static com.shuyu.gsyvideoplayer.utils.CommonUtil.hideNavKey;
import static com.shuyu.gsyvideoplayer.utils.CommonUtil.showNavKey;
S
1.1.6  
shuyu 已提交
27

S
shuyu 已提交
28 29
/**
 * Created by shuyu on 2016/11/12.
S
1.1.9  
shuyu 已提交
30 31
 * 列表工具类
 * 其中记得设置进来的fullViewContainer必须是在Activity布局下的最外层布局
S
shuyu 已提交
32 33 34 35 36 37
 */

public class ListVideoUtil {

    private String TAG = "NULL"; //播放的标志
    private StandardGSYVideoPlayer gsyVideoPlayer;
38
    private ViewGroup fullViewContainer;
39
    private ViewGroup listParent;//记录列表中item的父布局
S
1.1.6  
shuyu 已提交
40
    private ViewGroup.LayoutParams listParams;
S
shuyu 已提交
41
    private OrientationUtils orientationUtils;
S
1.1.9  
shuyu 已提交
42 43
    private StandardVideoAllCallBack videoAllCallBack;
    private String url;
44
    private Context context;
S
1.3.0  
shuyu 已提交
45 46 47 48 49
    private File cachePath;

    private Object[] objects;

    private Map<String, String> mapHeadData;
S
shuyu 已提交
50

51
    private int playPosition = -1; // 播放的位置
S
1.2.8  
shuyu 已提交
52
    private int speed = 1; // 播放速度,仅支持6.0
S
shuyu 已提交
53
    private int systemUiVisibility;
54
    private boolean isFull; //当前是否全屏
S
shuyu 已提交
55
    private boolean isSmall; //当前是否小屏
56 57
    private boolean hideStatusBar; //是否隐藏有状态bar
    private boolean hideActionBar; //是否隐藏有状态ActionBar
S
1.2.6  
shuyu 已提交
58
    private boolean isLoop;//循环
S
shuyu 已提交
59
    private boolean hideKey = true;//隐藏按键
S
shuyu 已提交
60
    private boolean needLockFull = true;//隐藏按键
S
shuyu 已提交
61

S
1.1.9  
shuyu 已提交
62

S
shuyu 已提交
63 64
    private int[] listItemRect;//当前item框的屏幕位置
    private int[] listItemSize;//当前item的大小
S
1.1.6  
shuyu 已提交
65

S
1.2.5  
shuyu 已提交
66 67 68 69 70

    private boolean fullLandFrist = true; //是否全屏就马上横屏
    private boolean autoRotation = true;//是否自动旋转
    private boolean showFullAnimation = true;//是否需要全屏动画

S
1.1.6  
shuyu 已提交
71 72 73
    private Handler handler = new Handler();


S
shuyu 已提交
74 75
    public ListVideoUtil(Context context) {
        gsyVideoPlayer = new StandardGSYVideoPlayer(context);
76
        this.context = context;
S
shuyu 已提交
77 78
    }

S
shuyu 已提交
79 80 81 82 83 84 85 86 87 88
    /**
     * 动态添加视频播放
     *
     * @param position  位置
     * @param imgView   封面
     * @param tag       TAG类型
     * @param container player的容器
     * @param playBtn   播放按键
     */
    public void addVideoPlayer(final int position, View imgView, String tag,
S
shuyu 已提交
89 90 91
                               ViewGroup container, View playBtn) {
        container.removeAllViews();
        if (isCurrentViewPlaying(position, tag)) {
92 93 94 95 96 97 98
            if (!isFull) {
                ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent();
                if (viewGroup != null)
                    viewGroup.removeAllViews();
                container.addView(gsyVideoPlayer);
                playBtn.setVisibility(View.INVISIBLE);
            }
S
shuyu 已提交
99 100 101
        } else {
            playBtn.setVisibility(View.VISIBLE);
            container.removeAllViews();   //增加封面
S
shuyu 已提交
102
            container.addView(imgView);
S
shuyu 已提交
103 104 105
        }
    }

106 107 108 109 110 111
    /**
     * 设置列表播放中的位置和TAG,防止错位,回复播放位置
     *
     * @param playPosition 列表中的播放位置
     * @param tag          播放的是哪个列表的tag
     */
S
shuyu 已提交
112 113 114 115 116
    public void setPlayPositionAndTag(int playPosition, String tag) {
        this.playPosition = playPosition;
        this.TAG = tag;
    }

117 118 119 120 121
    /**
     * 开始播放
     *
     * @param url 播放的URL
     */
S
shuyu 已提交
122
    public void startPlay(String url) {
S
1.1.7  
shuyu 已提交
123 124 125 126 127

        if (isSmall()) {
            smallVideoToNormal();
        }

S
1.1.9  
shuyu 已提交
128 129
        this.url = url;

S
shuyu 已提交
130 131
        gsyVideoPlayer.release();

S
1.2.6  
shuyu 已提交
132 133
        gsyVideoPlayer.setLooping(isLoop);

S
1.2.8  
shuyu 已提交
134 135
        gsyVideoPlayer.setSpeed(speed);

S
shuyu 已提交
136 137
        gsyVideoPlayer.setNeedLockFull(needLockFull);

S
1.3.0  
shuyu 已提交
138
        gsyVideoPlayer.setUp(url, true, cachePath, mapHeadData, objects);
S
shuyu 已提交
139 140 141 142 143 144 145 146

        //增加title
        gsyVideoPlayer.getTitleTextView().setVisibility(View.GONE);

        //设置返回键
        gsyVideoPlayer.getBackButton().setVisibility(View.GONE);

        //设置全屏按键功能
147
        gsyVideoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
S
shuyu 已提交
148 149
            @Override
            public void onClick(View v) {
150
                resolveFullBtn();
S
shuyu 已提交
151 152 153 154 155 156 157
            }
        });

        gsyVideoPlayer.startPlayLogic();
    }


158 159 160 161 162 163 164
    public void resolveFullBtn() {
        if (fullViewContainer == null) {
            return;
        }
        if (!isFull) {
            resolveToFull();
        } else {
S
1.1.6  
shuyu 已提交
165
            resolveMaterialToNormal(gsyVideoPlayer);
166 167 168
        }
    }

169 170 171
    /**
     * 处理全屏逻辑
     */
172
    private void resolveToFull() {
S
shuyu 已提交
173
        systemUiVisibility = ((Activity) context).getWindow().getDecorView().getSystemUiVisibility();
174
        CommonUtil.hideSupportActionBar(context, hideActionBar, hideStatusBar);
S
shuyu 已提交
175 176 177
        if (hideKey) {
            hideNavKey(context);
        }
178 179
        isFull = true;
        ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent();
S
1.1.6  
shuyu 已提交
180
        listParams = gsyVideoPlayer.getLayoutParams();
181 182 183 184
        if (viewGroup != null) {
            listParent = viewGroup;
            viewGroup.removeView(gsyVideoPlayer);
        }
S
1.1.4  
shuyu 已提交
185
        gsyVideoPlayer.setIfCurrentIsFullscreen(true);
186 187 188 189
        gsyVideoPlayer.getFullscreenButton().setImageResource(R.drawable.video_shrink);
        gsyVideoPlayer.getBackButton().setVisibility(View.VISIBLE);
        //设置旋转
        orientationUtils = new OrientationUtils((Activity) context, gsyVideoPlayer);
190
        orientationUtils.setEnable(isAutoRotation());
191 192 193
        gsyVideoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
S
1.1.6  
shuyu 已提交
194
                resolveMaterialToNormal(gsyVideoPlayer);
195 196
            }
        });
S
1.2.4  
shuyu 已提交
197
        if (showFullAnimation) {
S
1.1.6  
shuyu 已提交
198 199 200 201 202 203 204 205 206
            if (fullViewContainer instanceof FrameLayout) {
                //目前只做了frameLoayout的判断
                resolveMaterialAnimation();
            } else {
                resolveFullAdd();
            }

        } else {
            resolveFullAdd();
207
        }
208 209
    }

S
1.1.6  
shuyu 已提交
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
    /**
     * 添加到全屏父布局里
     */
    private void resolveFullAdd() {
        fullViewContainer.setBackgroundColor(Color.BLACK);
        fullViewContainer.addView(gsyVideoPlayer);
        resolveChangeFirstLogic(50);
    }

    /**
     * 如果是5.0的动画开始位置
     */
    private void resolveMaterialAnimation() {
        listItemRect = new int[2];
        listItemSize = new int[2];
        saveLocationStatus(context, hideStatusBar, hideActionBar);
        FrameLayout.LayoutParams lpParent = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        FrameLayout frameLayout = new FrameLayout(context);
        frameLayout.setBackgroundColor(Color.BLACK);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(listItemSize[0], listItemSize[1]);
        lp.setMargins(listItemRect[0], listItemRect[1], 0, 0);
        frameLayout.addView(gsyVideoPlayer, lp);
        fullViewContainer.addView(frameLayout, lpParent);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //开始动画
                TransitionManager.beginDelayedTransition(fullViewContainer);
                resolveMaterialFullVideoShow(gsyVideoPlayer);
                resolveChangeFirstLogic(600);
            }
        }, 300);
    }

    /**
     * 如果是5.0的,要从原位置过度到全屏位置
     */
    private void resolveMaterialFullVideoShow(GSYBaseVideoPlayer gsyVideoPlayer) {
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) gsyVideoPlayer.getLayoutParams();
        lp.setMargins(0, 0, 0, 0);
        lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.gravity = Gravity.CENTER;
        gsyVideoPlayer.setLayoutParams(lp);
        gsyVideoPlayer.setIfCurrentIsFullscreen(true);
    }


258 259 260
    /**
     * 处理正常逻辑
     */
261 262
    private void resolveToNormal() {
        int delay = orientationUtils.backToProtVideo();
S
1.1.6  
shuyu 已提交
263
        handler.postDelayed(new Runnable() {
264 265 266 267
            @Override
            public void run() {
                isFull = false;
                fullViewContainer.removeAllViews();
S
1.1.6  
shuyu 已提交
268 269 270
                if (gsyVideoPlayer.getParent() != null) {
                    ((ViewGroup) gsyVideoPlayer.getParent()).removeView(gsyVideoPlayer);
                }
271
                orientationUtils.setEnable(false);
S
1.1.4  
shuyu 已提交
272
                gsyVideoPlayer.setIfCurrentIsFullscreen(false);
273
                fullViewContainer.setBackgroundColor(Color.TRANSPARENT);
S
1.1.6  
shuyu 已提交
274
                listParent.addView(gsyVideoPlayer, listParams);
275 276
                gsyVideoPlayer.getFullscreenButton().setImageResource(R.drawable.video_enlarge);
                gsyVideoPlayer.getBackButton().setVisibility(View.GONE);
S
1.1.9  
shuyu 已提交
277 278 279 280 281
                gsyVideoPlayer.setIfCurrentIsFullscreen(false);
                if (videoAllCallBack != null) {
                    Debuger.printfLog("onQuitFullscreen");
                    videoAllCallBack.onQuitFullscreen(url);
                }
S
shuyu 已提交
282 283 284
                if (hideKey) {
                    showNavKey(context, systemUiVisibility);
                }
285
                CommonUtil.showSupportActionBar(context, hideActionBar, hideStatusBar);
286 287 288 289 290
            }
        }, delay);
    }


S
1.1.6  
shuyu 已提交
291 292 293 294
    /**
     * 动画回到正常效果
     */
    private void resolveMaterialToNormal(final GSYVideoPlayer gsyVideoPlayer) {
S
1.2.4  
shuyu 已提交
295
        if (showFullAnimation && fullViewContainer instanceof FrameLayout) {
S
1.1.6  
shuyu 已提交
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
            int delay = orientationUtils.backToProtVideo();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    TransitionManager.beginDelayedTransition(fullViewContainer);
                    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) gsyVideoPlayer.getLayoutParams();
                    lp.setMargins(listItemRect[0], listItemRect[1], 0, 0);
                    lp.width = listItemSize[0];
                    lp.height = listItemSize[1];
                    //注意配置回来,不然动画效果会不对
                    lp.gravity = Gravity.NO_GRAVITY;
                    gsyVideoPlayer.setLayoutParams(lp);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            resolveToNormal();
                        }
                    }, 400);
                }
            }, delay);
        } else {
            resolveToNormal();
        }
    }


    /**
     * 是否全屏一开始马上自动横屏
     */
    private void resolveChangeFirstLogic(int time) {
        if (isFullLandFrist()) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (orientationUtils.getIsLand() != 1) {
                        orientationUtils.resolveByClick();
                    }
                }
            }, time);
        }
S
1.1.9  
shuyu 已提交
336 337 338 339 340
        gsyVideoPlayer.setIfCurrentIsFullscreen(true);
        if (videoAllCallBack != null) {
            Debuger.printfLog("onEnterFullscreen");
            videoAllCallBack.onEnterFullscreen(this.url);
        }
S
1.1.6  
shuyu 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    }

    /**
     * 保存大小和状态
     */
    private void saveLocationStatus(Context context, boolean statusBar, boolean actionBar) {
        listParent.getLocationOnScreen(listItemRect);
        int statusBarH = getStatusBarHeight(context);
        int actionBerH = getActionBarHeight((Activity) context);
        if (statusBar) {
            listItemRect[1] = listItemRect[1] - statusBarH;
        }
        if (actionBar) {
            listItemRect[1] = listItemRect[1] - actionBerH;
        }
        listItemSize[0] = listParent.getWidth();
        listItemSize[1] = listParent.getHeight();
    }


361 362 363 364 365 366 367 368 369 370 371 372 373 374
    /**
     * 是否当前播放
     */
    private boolean isPlayView(int position, String tag) {
        return playPosition == position && TAG.equals(tag);
    }

    private boolean isCurrentViewPlaying(int position, String tag) {
        return isPlayView(position, tag);
    }

    /**
     * 处理返回正常逻辑
     */
375 376 377 378
    public boolean backFromFull() {
        boolean isFull = false;
        if (fullViewContainer.getChildCount() > 0) {
            isFull = true;
S
1.1.6  
shuyu 已提交
379
            resolveMaterialToNormal(gsyVideoPlayer);
380 381 382 383
        }
        return isFull;
    }

384 385 386
    /**
     * 释放持有的视频
     */
S
shuyu 已提交
387 388
    public void releaseVideoPlayer() {
        ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent();
S
shuyu 已提交
389 390
        if (viewGroup != null)
            viewGroup.removeAllViews();
S
shuyu 已提交
391 392
        playPosition = -1;
        TAG = "NULL";
S
shuyu 已提交
393 394
        if (orientationUtils != null)
            orientationUtils.releaseListener();
S
shuyu 已提交
395 396 397

    }

S
shuyu 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    /**
     * 显示小屏幕效果
     *
     * @param size      小视频的大小
     * @param actionBar 是否有actionBar
     * @param statusBar 是否有状态栏
     */
    public void showSmallVideo(Point size, final boolean actionBar, final boolean statusBar) {
        if (gsyVideoPlayer.getCurrentState() == GSYVideoPlayer.CURRENT_STATE_PLAYING) {
            gsyVideoPlayer.showSmallVideo(size, actionBar, statusBar);
            isSmall = true;
        }
    }


    /**
     * 恢复小屏幕效果
     */
    public void smallVideoToNormal() {
        isSmall = false;
        gsyVideoPlayer.hideSmallVideo();
    }


422 423 424 425 426 427 428
    /**
     * 设置全屏显示的viewGroup
     *
     * @param fullViewContainer viewGroup
     */
    public void setFullViewContainer(ViewGroup fullViewContainer) {
        this.fullViewContainer = fullViewContainer;
S
shuyu 已提交
429 430
    }

431 432 433 434 435
    /**
     * 是否全屏
     */
    public boolean isFull() {
        return isFull;
S
shuyu 已提交
436
    }
437

438 439 440 441 442 443 444 445 446
    /**
     * 是否自动旋转
     *
     * @param autoRotation 是否要支持重力旋转
     */
    public void setAutoRotation(boolean autoRotation) {
        this.autoRotation = autoRotation;
    }

S
1.2.5  
shuyu 已提交
447 448
    public boolean isAutoRotation() {
        return autoRotation;
449 450 451 452 453 454 455 456 457
    }

    /**
     * 是否全屏就马上横屏
     *
     * @param fullLandFrist 如果是,那么全屏的时候就会切换到横屏
     */
    public void setFullLandFrist(boolean fullLandFrist) {
        this.fullLandFrist = fullLandFrist;
458
    }
459

S
1.2.5  
shuyu 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    public boolean isFullLandFrist() {
        return fullLandFrist;
    }

    /**
     * 全屏动画
     *
     * @param showFullAnimation 是否使用全屏动画效果
     */
    public void setShowFullAnimation(boolean showFullAnimation) {
        this.showFullAnimation = showFullAnimation;
    }

    public boolean isShowFullAnimation() {
        return showFullAnimation;
    }


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
    public boolean isHideStatusBar() {
        return hideStatusBar;
    }

    /**
     * 是否隐藏statusBar
     *
     * @param hideStatusBar true的话会隐藏statusBar,在退出全屏的时候会回复显示
     */
    public void setHideStatusBar(boolean hideStatusBar) {
        this.hideStatusBar = hideStatusBar;
    }

    public boolean isHideActionBar() {
        return hideActionBar;
    }

    /**
     * 是否隐藏actionBar
     *
     * @param hideActionBar true的话会隐藏actionbar,在退出全屏的会回复时候显示
     */
    public void setHideActionBar(boolean hideActionBar) {
        this.hideActionBar = hideActionBar;
    }
S
1.1.6  
shuyu 已提交
503

S
1.1.9  
shuyu 已提交
504 505 506 507 508 509 510 511 512 513
    /**
     * 视频接口回调
     *
     * @param videoAllCallBack 回调
     */
    public void setVideoAllCallBack(StandardVideoAllCallBack videoAllCallBack) {
        this.videoAllCallBack = videoAllCallBack;
        gsyVideoPlayer.setStandardVideoAllCallBack(videoAllCallBack);
    }

S
shuyu 已提交
514 515 516 517 518 519 520 521 522 523 524
    public int getPlayPosition() {
        return playPosition;
    }

    public String getPlayTAG() {
        return TAG;
    }

    public boolean isSmall() {
        return isSmall;
    }
S
1.2.2  
shuyu 已提交
525 526


S
1.2.6  
shuyu 已提交
527 528 529 530 531 532 533 534 535 536 537
    public boolean isLoop() {
        return isLoop;
    }

    /**
     * 循环
     */
    public void setLoop(boolean loop) {
        isLoop = loop;
    }

S
1.2.2  
shuyu 已提交
538 539 540 541 542 543 544
    /**
     * 获取当前总时长
     */
    public int getDuration() {
        return gsyVideoPlayer.getDuration();
    }

S
1.2.8  
shuyu 已提交
545 546 547 548 549 550 551 552 553 554 555 556

    public int getSpeed() {
        return speed;
    }

    /**
     * 播放速度,仅支持6.0
     */
    public void setSpeed(int speed) {
        this.speed = speed;
    }

S
1.3.0  
shuyu 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

    public File getCachePath() {
        return cachePath;
    }

    /**
     * 缓存的路径
     */
    public void setCachePath(File cachePath) {
        this.cachePath = cachePath;
    }

    public Object[] getObjects() {
        return objects;
    }

    public void setObjects(Object[] objects) {
        this.objects = objects;
    }

    public Map<String, String> getMapHeadData() {
        return mapHeadData;
    }

    public void setMapHeadData(Map<String, String> mapHeadData) {
        this.mapHeadData = mapHeadData;
    }

S
1.2.2  
shuyu 已提交
585 586 587 588 589 590 591 592
    /**
     * 获取当前播放进度
     */
    public int getCurrentPositionWhenPlaying() {
        return gsyVideoPlayer.getCurrentPositionWhenPlaying();
    }

    /**
S
1.2.6  
shuyu 已提交
593
     * 获取播放器,直接拿播放器,根据需要自定义配置
S
1.2.2  
shuyu 已提交
594 595 596 597 598
     */
    public StandardGSYVideoPlayer getGsyVideoPlayer() {
        return gsyVideoPlayer;
    }

S
shuyu 已提交
599 600 601 602 603 604 605 606 607 608 609
    public boolean isHideKey() {
        return hideKey;
    }

    /**
     * 隐藏虚拟按键
     */
    public void setHideKey(boolean hideKey) {
        this.hideKey = hideKey;
    }

S
shuyu 已提交
610 611 612 613 614 615 616 617 618 619 620
    public boolean isNeedLockFull() {
        return needLockFull;
    }

    /**
     * 是否需要全屏锁定屏幕功能
     */
    public void setNeedLockFull(boolean needLoadFull) {
        this.needLockFull = needLoadFull;
    }

S
shuyu 已提交
621
}