ListVideoUtil.java 8.1 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 6 7 8
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.transition.TransitionManager;
S
shuyu 已提交
9 10 11 12
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

13
import com.shuyu.gsyvideoplayer.GSYVideoPlayer;
14
import com.shuyu.gsyvideoplayer.R;
S
shuyu 已提交
15 16 17 18 19 20 21 22 23 24
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

/**
 * Created by shuyu on 2016/11/12.
 */

public class ListVideoUtil {

    private String TAG = "NULL"; //播放的标志
    private StandardGSYVideoPlayer gsyVideoPlayer;
25
    private ViewGroup fullViewContainer;
26
    private ViewGroup listParent;//记录列表中item的父布局
S
shuyu 已提交
27
    private OrientationUtils orientationUtils;
28
    private Context context;
29 30
    private int playPosition = -1; // 播放的位置
    private boolean isFull; //当前是否全屏
31
    private boolean autoRotation = true;//是否自动旋转
32 33 34
    private boolean fullLandFrist = true; //是否全屏就马上横屏
    private boolean hideStatusBar; //是否隐藏有状态bar
    private boolean hideActionBar; //是否隐藏有状态ActionBar
S
shuyu 已提交
35 36 37

    public ListVideoUtil(Context context) {
        gsyVideoPlayer = new StandardGSYVideoPlayer(context);
38
        this.context = context;
S
shuyu 已提交
39 40
    }

S
shuyu 已提交
41 42 43 44 45 46 47 48 49 50
    /**
     * 动态添加视频播放
     *
     * @param position  位置
     * @param imgView   封面
     * @param tag       TAG类型
     * @param container player的容器
     * @param playBtn   播放按键
     */
    public void addVideoPlayer(final int position, View imgView, String tag,
S
shuyu 已提交
51 52 53
                               ViewGroup container, View playBtn) {
        container.removeAllViews();
        if (isCurrentViewPlaying(position, tag)) {
54 55 56 57 58 59 60
            if (!isFull) {
                ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent();
                if (viewGroup != null)
                    viewGroup.removeAllViews();
                container.addView(gsyVideoPlayer);
                playBtn.setVisibility(View.INVISIBLE);
            }
S
shuyu 已提交
61 62 63
        } else {
            playBtn.setVisibility(View.VISIBLE);
            container.removeAllViews();   //增加封面
S
shuyu 已提交
64
            container.addView(imgView);
S
shuyu 已提交
65 66 67
        }
    }

68 69 70 71 72 73
    /**
     * 设置列表播放中的位置和TAG,防止错位,回复播放位置
     *
     * @param playPosition 列表中的播放位置
     * @param tag          播放的是哪个列表的tag
     */
S
shuyu 已提交
74 75 76 77 78
    public void setPlayPositionAndTag(int playPosition, String tag) {
        this.playPosition = playPosition;
        this.TAG = tag;
    }

79 80 81 82 83
    /**
     * 开始播放
     *
     * @param url 播放的URL
     */
S
shuyu 已提交
84 85 86 87 88 89 90 91 92 93 94 95
    public void startPlay(String url) {
        gsyVideoPlayer.release();

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

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

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

        //设置全屏按键功能
96
        gsyVideoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
S
shuyu 已提交
97 98
            @Override
            public void onClick(View v) {
99
                resolveFullBtn();
S
shuyu 已提交
100 101 102 103 104 105 106
            }
        });

        gsyVideoPlayer.startPlayLogic();
    }


107 108 109 110 111 112 113 114 115 116 117
    public void resolveFullBtn() {
        if (fullViewContainer == null) {
            return;
        }
        if (!isFull) {
            resolveToFull();
        } else {
            resolveToNormal();
        }
    }

118 119 120
    /**
     * 处理全屏逻辑
     */
121
    private void resolveToFull() {
122
        CommonUtil.hideSupportActionBar(context, hideActionBar, hideStatusBar);
123 124 125 126 127 128 129 130
        isFull = true;
        ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent();
        if (viewGroup != null) {
            listParent = viewGroup;
            viewGroup.removeView(gsyVideoPlayer);
        }
        fullViewContainer.setBackgroundColor(Color.BLACK);
        fullViewContainer.addView(gsyVideoPlayer);
S
1.1.4  
shuyu 已提交
131
        gsyVideoPlayer.setIfCurrentIsFullscreen(true);
132 133 134 135
        gsyVideoPlayer.getFullscreenButton().setImageResource(R.drawable.video_shrink);
        gsyVideoPlayer.getBackButton().setVisibility(View.VISIBLE);
        //设置旋转
        orientationUtils = new OrientationUtils((Activity) context, gsyVideoPlayer);
136
        orientationUtils.setEnable(isAutoRotation());
137 138 139 140 141 142
        gsyVideoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resolveToNormal();
            }
        });
143 144 145 146 147 148 149
        if (isFullLandFrist()) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (orientationUtils.getIsLand() != 1) {
                        orientationUtils.resolveByClick();
                    }
150
                }
151 152
            }, 50);
        }
153 154
    }

155 156 157
    /**
     * 处理正常逻辑
     */
158
    private void resolveToNormal() {
159
        CommonUtil.showSupportActionBar(context, hideActionBar, hideStatusBar);
160 161 162 163 164 165 166
        int delay = orientationUtils.backToProtVideo();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isFull = false;
                fullViewContainer.removeAllViews();
                orientationUtils.setEnable(false);
S
1.1.4  
shuyu 已提交
167
                gsyVideoPlayer.setIfCurrentIsFullscreen(false);
168 169 170 171 172 173 174 175 176
                fullViewContainer.setBackgroundColor(Color.TRANSPARENT);
                listParent.addView(gsyVideoPlayer);
                gsyVideoPlayer.getFullscreenButton().setImageResource(R.drawable.video_enlarge);
                gsyVideoPlayer.getBackButton().setVisibility(View.GONE);
            }
        }, delay);
    }


177 178 179 180 181 182 183 184 185 186 187 188 189 190
    /**
     * 是否当前播放
     */
    private boolean isPlayView(int position, String tag) {
        return playPosition == position && TAG.equals(tag);
    }

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

    /**
     * 处理返回正常逻辑
     */
191 192 193 194 195 196 197 198 199
    public boolean backFromFull() {
        boolean isFull = false;
        if (fullViewContainer.getChildCount() > 0) {
            isFull = true;
            resolveToNormal();
        }
        return isFull;
    }

200 201 202
    /**
     * 释放持有的视频
     */
S
shuyu 已提交
203 204
    public void releaseVideoPlayer() {
        ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent();
S
shuyu 已提交
205 206
        if (viewGroup != null)
            viewGroup.removeAllViews();
S
shuyu 已提交
207 208 209 210 211
        playPosition = -1;
        TAG = "NULL";

    }

212 213 214 215 216 217 218
    /**
     * 设置全屏显示的viewGroup
     *
     * @param fullViewContainer viewGroup
     */
    public void setFullViewContainer(ViewGroup fullViewContainer) {
        this.fullViewContainer = fullViewContainer;
S
shuyu 已提交
219 220
    }

221 222 223 224 225
    /**
     * 是否全屏
     */
    public boolean isFull() {
        return isFull;
S
shuyu 已提交
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
    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;
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

    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
shuyu 已提交
279
}