DetailPlayer.java 6.4 KB
Newer Older
S
shuyu 已提交
1 2
package com.example.gsyvideoplayer;

S
1.3.6  
shuyu 已提交
3 4
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
5
import android.graphics.Point;
S
shuyu 已提交
6 7
import android.os.Bundle;
import android.os.Handler;
S
1.3.2  
shuyu 已提交
8
import android.support.v4.app.FragmentActivity;
S
shuyu 已提交
9 10 11 12 13 14 15
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

S
1.3.6  
shuyu 已提交
16
import com.example.gsyvideoplayer.listener.SampleListener;
S
shuyu 已提交
17
import com.shuyu.gsyvideoplayer.GSYVideoPlayer;
S
1.3.6  
shuyu 已提交
18
import com.shuyu.gsyvideoplayer.utils.Debuger;
S
shuyu 已提交
19 20
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
S
1.2.4  
shuyu 已提交
21
import com.transitionseverywhere.TransitionManager;
S
shuyu 已提交
22 23 24 25 26 27

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.gsyvideoplayer.utils.CommonUtil.setViewHeight;

S
1.3.2  
shuyu 已提交
28
public class DetailPlayer extends FragmentActivity {
S
shuyu 已提交
29 30 31 32 33 34 35 36 37

    @BindView(R.id.post_detail_nested_scroll)
    NestedScrollView postDetailNestedScroll;
    @BindView(R.id.detail_player)
    StandardGSYVideoPlayer detailPlayer;
    @BindView(R.id.activity_detail_player)
    RelativeLayout activityDetailPlayer;

    private boolean isFull;
S
1.3.6  
shuyu 已提交
38
    private boolean isPlay;
S
shuyu 已提交
39 40 41 42 43 44 45 46 47 48

    private OrientationUtils orientationUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_player);
        ButterKnife.bind(this);

        String url = "http://baobab.wdjcdn.com/14564977406580.mp4";
S
shuyu 已提交
49
        detailPlayer.setUp(url, true, null, "测试视频");
S
shuyu 已提交
50 51 52 53 54 55 56 57 58 59

        //增加封面
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(R.mipmap.xxx1);
        detailPlayer.setThumbImageView(imageView);

        resolveNormalVideoUI();

        orientationUtils = new OrientationUtils(this, detailPlayer);
S
1.3.6  
shuyu 已提交
60
        //初始化不打开外部的旋转
S
shuyu 已提交
61 62
        orientationUtils.setEnable(false);

S
1.1.4  
shuyu 已提交
63
        detailPlayer.setIsTouchWiget(true);
S
1.3.6  
shuyu 已提交
64 65 66
        //打开自动旋转
        detailPlayer.setRotateViewAuto(true);
        detailPlayer.setLockLand(false);
S
1.3.2  
shuyu 已提交
67 68
        detailPlayer.setShowFullAnimation(false);

S
shuyu 已提交
69 70 71
        detailPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
S
1.3.6  
shuyu 已提交
72 73 74
                //直接横屏
                orientationUtils.resolveByClick();

S
1.3.2  
shuyu 已提交
75 76
                //第一个true是否需要隐藏actionbar,第二个true是否需要隐藏statusbar
                detailPlayer.startWindowFullscreen(DetailPlayer.this, true, true);
S
1.3.6  
shuyu 已提交
77

S
1.3.2  
shuyu 已提交
78 79
                //这是以前旧的方式
                //toDo();
S
shuyu 已提交
80 81 82 83 84 85 86 87 88 89
            }
        });

        detailPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toNormal();
            }
        });

S
1.3.6  
shuyu 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        detailPlayer.setStandardVideoAllCallBack(new SampleListener() {
            @Override
            public void onPrepared(String url, Object... objects) {
                super.onPrepared(url, objects);
                //开始播放了才能旋转和全屏
                orientationUtils.setEnable(true);
                isPlay = true;
            }

            @Override
            public void onAutoComplete(String url, Object... objects) {
                super.onAutoComplete(url, objects);
            }

            @Override
            public void onClickStartError(String url, Object... objects) {
                super.onClickStartError(url, objects);
            }
        });

S
shuyu 已提交
110 111 112 113
    }

    @Override
    public void onBackPressed() {
114 115 116 117 118

        if (orientationUtils != null) {
            orientationUtils.backToProtVideo();
        }

S
1.3.3  
shuyu 已提交
119
        if (StandardGSYVideoPlayer.backFromWindowFull(this)) {
S
shuyu 已提交
120 121 122 123 124 125 126 127 128
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoPlayer.releaseAllVideos();
S
1.3.3  
shuyu 已提交
129 130
        if (orientationUtils != null)
            orientationUtils.releaseListener();
S
shuyu 已提交
131 132
    }

S
1.3.6  
shuyu 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //如果旋转了就全屏
        if (isPlay) {
            if (newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_USER) {
                if (!detailPlayer.isIfCurrentIsFullscreen()) {
                    detailPlayer.startWindowFullscreen(DetailPlayer.this, true, true);
                }
            } else {
                //新版本isIfCurrentIsFullscreen的标志位内部提前设置了,所以不会和手动点击冲突
                if (detailPlayer.isIfCurrentIsFullscreen()) {
                    StandardGSYVideoPlayer.backFromWindowFull(this);
                }
            }
        }
    }

S
shuyu 已提交
151 152
    private void toFull() {
        isFull = true;
S
1.2.4  
shuyu 已提交
153 154 155

        TransitionManager.beginDelayedTransition(activityDetailPlayer);

S
shuyu 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168
        setViewHeight(detailPlayer, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        resolveFullVideoUI();
        orientationUtils.setEnable(true);
    }

    private void toNormal() {
        isFull = false;
        orientationUtils.setEnable(false);
        int delay = orientationUtils.backToProtVideo();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
S
1.2.4  
shuyu 已提交
169
                TransitionManager.beginDelayedTransition(activityDetailPlayer);
S
shuyu 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
                setViewHeight(detailPlayer, ViewGroup.LayoutParams.MATCH_PARENT,
                        (int) getResources().getDimension(R.dimen.post_media_height));
            }
        }, delay);
    }

    private void toDo() {
        if (isFull) {
            toNormal();
        } else {
            toFull();
        }
    }

    private void resolveNormalVideoUI() {
        //增加title
        detailPlayer.getTitleTextView().setVisibility(View.GONE);
        detailPlayer.getTitleTextView().setText("测试视频");
        detailPlayer.getBackButton().setVisibility(View.GONE);
    }

    private void resolveFullVideoUI() {
        detailPlayer.getTitleTextView().setVisibility(View.VISIBLE);
        detailPlayer.getTitleTextView().setText("测试视频");
        detailPlayer.getBackButton().setVisibility(View.VISIBLE);
    }

}