ListVideoUtil.java 6.9 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.R;
S
shuyu 已提交
14 15 16 17 18 19 20 21 22 23
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

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

public class ListVideoUtil {

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

    public ListVideoUtil(Context context) {
        gsyVideoPlayer = new StandardGSYVideoPlayer(context);
35
        this.context = context;
S
shuyu 已提交
36 37
    }

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

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

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

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

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

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

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

        gsyVideoPlayer.startPlayLogic();
    }


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

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

148 149 150
    /**
     * 处理正常逻辑
     */
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    private void resolveToNormal() {
        int delay = orientationUtils.backToProtVideo();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isFull = false;
                fullViewContainer.removeAllViews();
                orientationUtils.setEnable(false);
                fullViewContainer.setBackgroundColor(Color.TRANSPARENT);
                listParent.addView(gsyVideoPlayer);
                gsyVideoPlayer.getFullscreenButton().setImageResource(R.drawable.video_enlarge);
                gsyVideoPlayer.getBackButton().setVisibility(View.GONE);
            }
        }, delay);
    }


168 169 170 171 172 173 174 175 176 177 178 179 180 181
    /**
     * 是否当前播放
     */
    private boolean isPlayView(int position, String tag) {
        return playPosition == position && TAG.equals(tag);
    }

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

    /**
     * 处理返回正常逻辑
     */
182 183 184 185 186 187 188 189 190
    public boolean backFromFull() {
        boolean isFull = false;
        if (fullViewContainer.getChildCount() > 0) {
            isFull = true;
            resolveToNormal();
        }
        return isFull;
    }

191 192 193
    /**
     * 释放持有的视频
     */
S
shuyu 已提交
194 195
    public void releaseVideoPlayer() {
        ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent();
S
shuyu 已提交
196 197
        if (viewGroup != null)
            viewGroup.removeAllViews();
S
shuyu 已提交
198 199 200 201 202
        playPosition = -1;
        TAG = "NULL";

    }

203 204 205 206 207 208 209
    /**
     * 设置全屏显示的viewGroup
     *
     * @param fullViewContainer viewGroup
     */
    public void setFullViewContainer(ViewGroup fullViewContainer) {
        this.fullViewContainer = fullViewContainer;
S
shuyu 已提交
210 211
    }

212 213 214 215 216
    /**
     * 是否全屏
     */
    public boolean isFull() {
        return isFull;
S
shuyu 已提交
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
    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;
243
    }
S
shuyu 已提交
244
}