ListVideoUtil.java 2.9 KB
Newer Older
S
shuyu 已提交
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
package com.shuyu.gsyvideoplayer.utils;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

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

public class ListVideoUtil {


    private static ListVideoUtil listVideoUtil;

    public static synchronized ListVideoUtil getInstance(Context context) {
        if (listVideoUtil == null) {
            listVideoUtil = new ListVideoUtil(context);
        }
        return listVideoUtil;
    }

    private int playPosition = -1; // 播放的位置
    private String TAG = "NULL"; //播放的标志
    private StandardGSYVideoPlayer gsyVideoPlayer;
    private OrientationUtils orientationUtils;

    public ListVideoUtil(Context context) {
        gsyVideoPlayer = new StandardGSYVideoPlayer(context);
    }

    public void addVideoPlayer(Context context, final int position, int imgId, String tag,
                               ViewGroup container, View playBtn) {
        container.removeAllViews();
        if (isCurrentViewPlaying(position, tag)) {
            ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent();
            if (viewGroup != null)
                viewGroup.removeAllViews();
            container.addView(gsyVideoPlayer);
            playBtn.setVisibility(View.INVISIBLE);
        } else {
            playBtn.setVisibility(View.VISIBLE);
            container.removeAllViews();   //增加封面
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageResource(imgId);
            container.addView(imageView);
        }
    }

    public void setPlayPositionAndTag(int playPosition, String tag) {
        this.playPosition = playPosition;
        this.TAG = tag;
    }

    public void startPlay(String url) {
        gsyVideoPlayer.release();

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

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

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

        //设置全屏按键功能
S
shuyu 已提交
71
        gsyVideoPlayer.getmFullscreenButton().setOnClickListener(new View.OnClickListener() {
S
shuyu 已提交
72 73 74 75 76 77 78 79 80 81 82 83
            @Override
            public void onClick(View v) {
                //resolveFullBtn(holder.standardGSYVideoPlayer);
            }
        });

        gsyVideoPlayer.startPlayLogic();
    }


    public void releaseVideoPlayer() {
        ViewGroup viewGroup = (ViewGroup) gsyVideoPlayer.getParent();
S
shuyu 已提交
84 85
        if (viewGroup != null)
            viewGroup.removeAllViews();
S
shuyu 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
        playPosition = -1;
        TAG = "NULL";

    }

    private boolean isPlayView(int position, String tag) {
        return playPosition == position && TAG.equals(tag);
    }

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