ListVideoUtil.java 14.4 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 8 9
import android.os.Build;
import android.os.Handler;
import android.transition.TransitionManager;
S
1.1.6  
shuyu 已提交
10
import android.view.Gravity;
S
shuyu 已提交
11 12
import android.view.View;
import android.view.ViewGroup;
S
1.1.6  
shuyu 已提交
13
import android.widget.FrameLayout;
S
shuyu 已提交
14

15
import com.shuyu.gsyvideoplayer.GSYVideoPlayer;
16
import com.shuyu.gsyvideoplayer.R;
S
1.1.6  
shuyu 已提交
17
import com.shuyu.gsyvideoplayer.video.GSYBaseVideoPlayer;
S
shuyu 已提交
18 19
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

S
1.1.6  
shuyu 已提交
20 21 22
import static com.shuyu.gsyvideoplayer.utils.CommonUtil.getActionBarHeight;
import static com.shuyu.gsyvideoplayer.utils.CommonUtil.getStatusBarHeight;

S
shuyu 已提交
23 24 25 26 27 28 29 30
/**
 * Created by shuyu on 2016/11/12.
 */

public class ListVideoUtil {

    private String TAG = "NULL"; //播放的标志
    private StandardGSYVideoPlayer gsyVideoPlayer;
31
    private ViewGroup fullViewContainer;
32
    private ViewGroup listParent;//记录列表中item的父布局
S
1.1.6  
shuyu 已提交
33
    private ViewGroup.LayoutParams listParams;
S
shuyu 已提交
34
    private OrientationUtils orientationUtils;
35
    private Context context;
S
shuyu 已提交
36

37 38
    private int playPosition = -1; // 播放的位置
    private boolean isFull; //当前是否全屏
S
shuyu 已提交
39
    private boolean isSmall; //当前是否小屏
40
    private boolean autoRotation = true;//是否自动旋转
41 42 43
    private boolean fullLandFrist = true; //是否全屏就马上横屏
    private boolean hideStatusBar; //是否隐藏有状态bar
    private boolean hideActionBar; //是否隐藏有状态ActionBar
S
shuyu 已提交
44

S
shuyu 已提交
45 46
    private int[] listItemRect;//当前item框的屏幕位置
    private int[] listItemSize;//当前item的大小
S
1.1.6  
shuyu 已提交
47 48 49 50 51

    private Handler handler = new Handler();

    private boolean showFullAnimation = true;

S
shuyu 已提交
52 53
    public ListVideoUtil(Context context) {
        gsyVideoPlayer = new StandardGSYVideoPlayer(context);
54
        this.context = context;
S
shuyu 已提交
55 56
        int smallVideoWidth = CommonUtil.getScreenWidth(context) / 2 - CommonUtil.dip2px(context, 20);
        int smallVideoHeight = smallVideoWidth * 3 / 4;
S
shuyu 已提交
57 58
    }

S
shuyu 已提交
59 60 61 62 63 64 65 66 67 68
    /**
     * 动态添加视频播放
     *
     * @param position  位置
     * @param imgView   封面
     * @param tag       TAG类型
     * @param container player的容器
     * @param playBtn   播放按键
     */
    public void addVideoPlayer(final int position, View imgView, String tag,
S
shuyu 已提交
69 70 71
                               ViewGroup container, View playBtn) {
        container.removeAllViews();
        if (isCurrentViewPlaying(position, tag)) {
72 73 74 75 76 77 78
            if (!isFull) {
                ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent();
                if (viewGroup != null)
                    viewGroup.removeAllViews();
                container.addView(gsyVideoPlayer);
                playBtn.setVisibility(View.INVISIBLE);
            }
S
shuyu 已提交
79 80 81
        } else {
            playBtn.setVisibility(View.VISIBLE);
            container.removeAllViews();   //增加封面
S
shuyu 已提交
82
            container.addView(imgView);
S
shuyu 已提交
83 84 85
        }
    }

86 87 88 89 90 91
    /**
     * 设置列表播放中的位置和TAG,防止错位,回复播放位置
     *
     * @param playPosition 列表中的播放位置
     * @param tag          播放的是哪个列表的tag
     */
S
shuyu 已提交
92 93 94 95 96
    public void setPlayPositionAndTag(int playPosition, String tag) {
        this.playPosition = playPosition;
        this.TAG = tag;
    }

97 98 99 100 101
    /**
     * 开始播放
     *
     * @param url 播放的URL
     */
S
shuyu 已提交
102
    public void startPlay(String url) {
S
1.1.7  
shuyu 已提交
103 104 105 106 107

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

S
shuyu 已提交
108 109 110 111 112 113 114 115 116 117 118
        gsyVideoPlayer.release();

        gsyVideoPlayer.setUp(url, true, "");

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

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

        //设置全屏按键功能
119
        gsyVideoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
S
shuyu 已提交
120 121
            @Override
            public void onClick(View v) {
122
                resolveFullBtn();
S
shuyu 已提交
123 124 125 126 127 128 129
            }
        });

        gsyVideoPlayer.startPlayLogic();
    }


130 131 132 133 134 135 136
    public void resolveFullBtn() {
        if (fullViewContainer == null) {
            return;
        }
        if (!isFull) {
            resolveToFull();
        } else {
S
1.1.6  
shuyu 已提交
137
            resolveMaterialToNormal(gsyVideoPlayer);
138 139 140
        }
    }

141 142 143
    /**
     * 处理全屏逻辑
     */
144
    private void resolveToFull() {
145
        CommonUtil.hideSupportActionBar(context, hideActionBar, hideStatusBar);
146 147
        isFull = true;
        ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent();
S
1.1.6  
shuyu 已提交
148
        listParams = gsyVideoPlayer.getLayoutParams();
149 150 151 152
        if (viewGroup != null) {
            listParent = viewGroup;
            viewGroup.removeView(gsyVideoPlayer);
        }
S
1.1.4  
shuyu 已提交
153
        gsyVideoPlayer.setIfCurrentIsFullscreen(true);
154 155 156 157
        gsyVideoPlayer.getFullscreenButton().setImageResource(R.drawable.video_shrink);
        gsyVideoPlayer.getBackButton().setVisibility(View.VISIBLE);
        //设置旋转
        orientationUtils = new OrientationUtils((Activity) context, gsyVideoPlayer);
158
        orientationUtils.setEnable(isAutoRotation());
159 160 161
        gsyVideoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
S
1.1.6  
shuyu 已提交
162
                resolveMaterialToNormal(gsyVideoPlayer);
163 164
            }
        });
S
1.1.6  
shuyu 已提交
165 166 167 168 169 170 171 172 173 174
        if (showFullAnimation && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (fullViewContainer instanceof FrameLayout) {
                //目前只做了frameLoayout的判断
                resolveMaterialAnimation();
            } else {
                resolveFullAdd();
            }

        } else {
            resolveFullAdd();
175
        }
176 177
    }

S
1.1.6  
shuyu 已提交
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
    /**
     * 添加到全屏父布局里
     */
    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);
    }


226 227 228
    /**
     * 处理正常逻辑
     */
229
    private void resolveToNormal() {
230
        CommonUtil.showSupportActionBar(context, hideActionBar, hideStatusBar);
231
        int delay = orientationUtils.backToProtVideo();
S
1.1.6  
shuyu 已提交
232
        handler.postDelayed(new Runnable() {
233 234 235 236
            @Override
            public void run() {
                isFull = false;
                fullViewContainer.removeAllViews();
S
1.1.6  
shuyu 已提交
237 238 239
                if (gsyVideoPlayer.getParent() != null) {
                    ((ViewGroup) gsyVideoPlayer.getParent()).removeView(gsyVideoPlayer);
                }
240
                orientationUtils.setEnable(false);
S
1.1.4  
shuyu 已提交
241
                gsyVideoPlayer.setIfCurrentIsFullscreen(false);
242
                fullViewContainer.setBackgroundColor(Color.TRANSPARENT);
S
1.1.6  
shuyu 已提交
243
                listParent.addView(gsyVideoPlayer, listParams);
244 245 246 247 248 249 250
                gsyVideoPlayer.getFullscreenButton().setImageResource(R.drawable.video_enlarge);
                gsyVideoPlayer.getBackButton().setVisibility(View.GONE);
            }
        }, delay);
    }


S
1.1.6  
shuyu 已提交
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
    /**
     * 动画回到正常效果
     */
    private void resolveMaterialToNormal(final GSYVideoPlayer gsyVideoPlayer) {
        if (showFullAnimation && fullViewContainer instanceof FrameLayout && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            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);
        }
    }

    /**
     * 保存大小和状态
     */
    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();
    }


316 317 318 319 320 321 322 323 324 325 326 327 328 329
    /**
     * 是否当前播放
     */
    private boolean isPlayView(int position, String tag) {
        return playPosition == position && TAG.equals(tag);
    }

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

    /**
     * 处理返回正常逻辑
     */
330 331 332 333
    public boolean backFromFull() {
        boolean isFull = false;
        if (fullViewContainer.getChildCount() > 0) {
            isFull = true;
S
1.1.6  
shuyu 已提交
334
            resolveMaterialToNormal(gsyVideoPlayer);
335 336 337 338
        }
        return isFull;
    }

339 340 341
    /**
     * 释放持有的视频
     */
S
shuyu 已提交
342 343
    public void releaseVideoPlayer() {
        ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent();
S
shuyu 已提交
344 345
        if (viewGroup != null)
            viewGroup.removeAllViews();
S
shuyu 已提交
346 347 348 349 350
        playPosition = -1;
        TAG = "NULL";

    }

S
shuyu 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    /**
     * 显示小屏幕效果
     *
     * @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();
    }


375 376 377 378 379 380 381
    /**
     * 设置全屏显示的viewGroup
     *
     * @param fullViewContainer viewGroup
     */
    public void setFullViewContainer(ViewGroup fullViewContainer) {
        this.fullViewContainer = fullViewContainer;
S
shuyu 已提交
382 383
    }

384 385 386 387 388
    /**
     * 是否全屏
     */
    public boolean isFull() {
        return isFull;
S
shuyu 已提交
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
    public boolean isAutoRotation() {
        return autoRotation;
    }

    /**
     * 是否自动旋转
     *
     * @param autoRotation 是否要支持重力旋转
     */
    public void setAutoRotation(boolean autoRotation) {
        this.autoRotation = autoRotation;
    }

    public boolean isFullLandFrist() {
        return fullLandFrist;
    }

    /**
     * 是否全屏就马上横屏
     *
     * @param fullLandFrist 如果是,那么全屏的时候就会切换到横屏
     */
    public void setFullLandFrist(boolean fullLandFrist) {
        this.fullLandFrist = fullLandFrist;
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

    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 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455


    public boolean isShowFullAnimation() {
        return showFullAnimation;
    }

    /**
     * 全屏动画
     *
     * @param showFullAnimation 是否使用全屏动画效果
     */
    public void setShowFullAnimation(boolean showFullAnimation) {
        this.showFullAnimation = showFullAnimation;
    }
S
shuyu 已提交
456 457 458 459 460 461 462 463 464 465 466 467

    public int getPlayPosition() {
        return playPosition;
    }

    public String getPlayTAG() {
        return TAG;
    }

    public boolean isSmall() {
        return isSmall;
    }
S
shuyu 已提交
468
}