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 List<BannerBean> videoList;
|
private boolean isPlay;
|
private boolean isPause;
|
|
public VideoFragment() {
|
}
|
|
public static VideoFragment newInstance() {
|
VideoFragment fragment = new VideoFragment();
|
Bundle args = new Bundle();
|
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) {
|
videoList = (List<BannerBean>) getArguments().getSerializable("videoList");
|
}
|
}
|
|
@Override
|
protected void initView() {
|
String playUrl = null;
|
for (BannerBean bannerBean : videoList) {
|
if (bannerBean.getChoose() == 1){
|
playUrl = bannerBean.getUrl();
|
break;
|
}
|
}
|
if (playUrl != null){
|
binding.videoPlayer.setVisibility(View.VISIBLE);
|
binding.layoutDataNull.setVisibility(View.GONE);
|
|
|
//外部辅助的旋转,帮助全屏
|
//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(false)
|
.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();
|
// }
|
}
|
})
|
.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);
|
}
|
});
|
}else {
|
binding.videoPlayer.setVisibility(View.GONE);
|
binding.layoutDataNull.setVisibility(View.VISIBLE);
|
}
|
}
|
|
@Override
|
protected void initData() {
|
|
}
|
|
public void setVideoList(List<BannerBean> videoList) {
|
this.videoList = videoList;
|
Bundle args = new Bundle();
|
args.putSerializable("videoList", (Serializable) videoList);
|
setArguments(args);
|
if (isAdded()){
|
initView();
|
}
|
}
|
|
public List<BannerBean> getVideoList() {
|
return videoList;
|
}
|
|
@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;
|
}
|
|
@Override
|
public void onResume() {
|
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);
|
// }
|
}
|
}
|