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.BaseConfig; 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 { 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 url) { VideoFragment fragment = new VideoFragment(); if (url.contains("profile/upload")) {//如果是上传到服务器的图片 //只取profile/upload以后的部分 url = url.substring(url.indexOf("profile/upload")); url = BaseConfig.BASE_URL_DOMAIN + "/" + url; } Bundle args = new Bundle(); args.putSerializable("playUrl", url); 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(); } }