Linjiajia
2023-10-07 31b7700b976a46901f67c5d7a00281ca4745fc9f
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
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
226
227
228
229
230
231
232
233
234
235
package com.application.zhangshi_app_android.ui.function;
 
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
 
 
import androidx.annotation.NonNull;
 
import com.android.app_base.base.view.BaseFragment;
import com.android.app_base.utils.GlideUtil;
import com.application.zhangshi_app_android.R;
import com.application.zhangshi_app_android.bean.BannerBean;
import com.application.zhangshi_app_android.databinding.FragmentVideoBinding;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.listener.LockClickListener;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author Ljj
 * @date 2023.07.17. 19:39
 * @desc
 */
public class VideoFragment extends BaseFragment<FragmentVideoBinding, VideoFragmentViewModel> {
    OrientationUtils orientationUtils;
    private boolean isPlay;
    private boolean isPause;
    private boolean isNeedStart = true;//是否需要开始播放
    private String playUrl;
    private onPlayListener onPlayListener;
    public VideoFragment() {
    }
 
    public static VideoFragment newInstance(String playUrl) {
        VideoFragment fragment = new VideoFragment();
        Bundle args = new Bundle();
        args.putSerializable("playUrl", playUrl);
        fragment.setArguments(args);
        return fragment;
    }
 
 
 
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video;
    }
 
    @Override
    protected int getVariableId() {
        return 0;
    }
 
    @Override
    protected void initParam() {
        if (getArguments() != null) {
            playUrl = (String) getArguments().getSerializable("playUrl");
        }
    }
 
    @Override
    protected void initView() {
        if (playUrl != null){
            //外部辅助的旋转,帮助全屏
            //orientationUtils = new OrientationUtils(this, binding.videoPlayer);
            //初始化不打开外部的旋转
            // orientationUtils.setEnable(false);
 
            //增加封面
            ImageView imageView = new ImageView(getSelfActivity());
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            GlideUtil.loadVideoCover(playUrl, imageView,-1,1);
            //增加title
            binding.videoPlayer.getTitleTextView().setVisibility(View.GONE);
            binding.videoPlayer.getBackButton().setVisibility(View.GONE);
            GSYVideoOptionBuilder gsyVideoOption = new GSYVideoOptionBuilder();
            gsyVideoOption.setThumbImageView(imageView)
                    .setIsTouchWiget(true)
                    .setRotateViewAuto(false)
                    .setLockLand(false)
                    .setAutoFullWithSize(false)
                    .setShowFullAnimation(false)
                    .setUrl(playUrl)
                    .setNeedLockFull(true)
                    .setCacheWithPlay(true)
                    .setVideoTitle("")
                    .setLooping(false)
                    ///不需要旋转
                    .setNeedOrientationUtils(false)
                    .setVideoAllCallBack(new GSYSampleCallBack() {
                        @Override
                        public void onPrepared(String url, Object... objects) {
                            super.onPrepared(url, objects);
                            //开始播放了才能旋转和全屏
//                        orientationUtils.setEnable(binding.videoPlayer.isRotateWithSystem());
                            isPlay = true;
                        }
 
                        @Override
                        public void onQuitFullscreen(String url, Object... objects) {
                            super.onQuitFullscreen(url, objects);
                            // ------- !!!如果不需要旋转屏幕,可以不调用!!!-------
                            // 不需要屏幕旋转,还需要设置 setNeedOrientationUtils(false)
//                        if (orientationUtils != null) {
//                            orientationUtils.backToProtVideo();
//                        }
                        }
 
                        @Override
                        public void onAutoComplete(String url, Object... objects) {
                            super.onAutoComplete(url, objects);
                            if (onPlayListener != null){
                                onPlayListener.onComplete();
                            }
                            isNeedStart = true;
                        }
 
                        @Override
                        public void onComplete(String url, Object... objects) {
                            super.onComplete(url, objects);
//                            isNeedStart = true;
                        }
                    })
                    .setLockClickListener(new LockClickListener() {
                        @Override
                        public void onClick(View view, boolean lock) {
//                        if (orientationUtils != null) {
//                            //配合下方的onConfigurationChanged
//                            orientationUtils.setEnable(!lock);
//                        }
                        }
                    }).build(binding.videoPlayer);
 
            binding.videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //直接横屏
                    // ------- !!!如果不需要旋转屏幕,可以不调用!!!-------
                    // 不需要屏幕旋转,还需要设置 setNeedOrientationUtils(false)
                    // orientationUtils.resolveByClick();
                    //第一个true是否需要隐藏actionbar,第二个true是否需要隐藏statusbar
                    binding.videoPlayer.startWindowFullscreen(getSelfActivity(), true, true);
                }
            });
        }
    }
 
    @Override
    protected void initData() {
 
    }
 
 
 
    @Override
    protected void initLiveDataObserve() {
 
    }
 
//    @Override
//    public void onBackPressed() {
//        // ------- !!!如果不需要旋转屏幕,可以不调用!!!-------
//        // 不需要屏幕旋转,还需要设置 setNeedOrientationUtils(false)
////        if (orientationUtils != null) {
////            orientationUtils.backToProtVideo();
////        }
//        if (GSYVideoManager.backFromWindowFull(this)) {
//            return;
//        }
//        super.onBackPressed();
//    }
 
 
    @Override
    public void onPause() {
        binding.videoPlayer.getCurrentPlayer().onVideoPause();
        super.onPause();
        isPause = true;
        System.out.println("onPause");
    }
 
 
    @Override
    public void onResume() {
        if (isNeedStart){
            binding.videoPlayer.getCurrentPlayer().startPlayLogic();
            isNeedStart = false;
        }else {
            binding.videoPlayer.getCurrentPlayer().onVideoResume(false);
        }
        super.onResume();
        isPause = false;
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (isPlay) {
            binding.videoPlayer.getCurrentPlayer().release();
        }
//        if (orientationUtils != null)
//            orientationUtils.releaseListener();
    }
 
 
    /**
     * orientationUtils 和  binding.videoPlayer.onConfigurationChanged 方法是用于触发屏幕旋转的
     */
    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //如果旋转了就全屏
//        if (isPlay && !isPause) {
//            binding.videoPlayer.onConfigurationChanged(this, newConfig, orientationUtils, true, true);
//        }
    }
 
    public void setOnPlayListener(onPlayListener listener){
        this.onPlayListener = listener;
    }
    public void startPlay(){
        isNeedStart = true;
    }
 
    public interface onPlayListener{
        void onComplete();
    }
}