PlayHelper.java 10.2 KB
/*
 * Copyright (c) Wondertek Technologies Co., Ltd. 2019-2022. All rights reserved.
 */

package com.wd.player.playerutil;

import android.content.res.Configuration;
import android.net.Uri;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.fragment.app.FragmentActivity;

import com.aliyun.player.IPlayer;
import com.wd.foundation.wdkit.system.DeviceUtil;
import com.wd.player.constant.PlayParameter;
import com.wd.player.widget.AliyunRenderView;
import com.wd.player.widget.AliyunScreenMode;
import com.wd.player.widget.VideoAndLivePlayerView;

/**
 * 描述:
 *
 * @author : lvjinhui
 * @since: 2022/6/22
 */
public class PlayHelper {
    /**
     * 精准seek开启判断逻辑:当视频时长小于5分钟的时候。
     */
    private static final int ACCURATE = 5 * 60 * 1000;

    /**
     * 播放器,不带控制层
     */
    private AliyunRenderView mAliyunRenderView;

    /**
     * 播放器,带控制层
     */
    private VideoAndLivePlayerView playerView;

    private FragmentActivity mContext;

    public static final class PlayHelperCreator {
        public static final PlayHelper playHelper = new PlayHelper();
    }

    public static PlayHelper getInstance() {
        return PlayHelperCreator.playHelper;
    }

    /**
     * 设置播放器view
     * 
     * @param playerView
     */
    public void setPlayerView(VideoAndLivePlayerView playerView) {
        this.playerView = playerView;
    }

    public void setAliyunRenderView(AliyunRenderView mAliyunRenderView) {
        this.mAliyunRenderView = mAliyunRenderView;
    }

    public void setContext(FragmentActivity mContext) {
        this.mContext = mContext;
    }

    /**
     * 判断是否开启精准seek
     */
    public void isAutoAccurate(long position) {
        if (mAliyunRenderView == null) {
            return;
        }
        if (getDuration() <= ACCURATE) {
            mAliyunRenderView.seekTo(position, IPlayer.SeekMode.Accurate);
        } else {
            mAliyunRenderView.seekTo(position, IPlayer.SeekMode.Inaccurate);
        }
    }

    /**
     * 获取视频时长
     *
     * @return 视频时长
     */
    public int getDuration() {
        if (mAliyunRenderView != null) {
            return (int) mAliyunRenderView.getDuration();
        }

        return 0;
    }

    /**
     * 目标位置计算算法
     *
     * @param duration 视频总时长
     * @param currentPosition 当前播放位置
     * @param deltaPosition 与当前位置相差的时长
     * @return
     */
    public static int getTargetPosition(long duration, long currentPosition, long deltaPosition) {
        // seek步长
        long finalDeltaPosition;
        // 根据视频时长,决定seek步长
        long totalMinutes = duration / 1000 / 60;
        int hours = (int) (totalMinutes / 60);
        int minutes = (int) (totalMinutes % 60);

        // 视频时长为1小时以上,小屏和全屏的手势滑动最长为视频时长的十分之一
        if (hours >= 1) {
            finalDeltaPosition = deltaPosition / 10;
        }// 视频时长为31分钟-60分钟时,小屏和全屏的手势滑动最长为视频时长五分之一
        else if (minutes > 30) {
            finalDeltaPosition = deltaPosition / 5;
        }// 视频时长为11分钟-30分钟时,小屏和全屏的手势滑动最长为视频时长三分之一
        else if (minutes > 10) {
            finalDeltaPosition = deltaPosition / 3;
        }// 视频时长为4-10分钟时,小屏和全屏的手势滑动最长为视频时长二分之一
        else if (minutes > 3) {
            finalDeltaPosition = deltaPosition / 2;
        }// 视频时长为1秒钟至3分钟时,小屏和全屏的手势滑动最长为视频结束
        else {
            finalDeltaPosition = deltaPosition;
        }

        long targetPosition = finalDeltaPosition + currentPosition;
        if (targetPosition < 0) {
            targetPosition = 0;
        }
        if (targetPosition > duration) {
            targetPosition = duration;
        }
        return (int) targetPosition;
    }

    /**
     * 判断是否是本地资源
     *
     * @return
     */
    public boolean isLocalSource() {
        String scheme = null;
        if ("vidsts".equals(PlayParameter.PLAY_PARAM_TYPE)) {
            return false;
        }
        if ("localSource".equals(PlayParameter.PLAY_PARAM_TYPE)) {
            Uri parse = Uri.parse(PlayParameter.PLAY_PARAM_URL_VIDEO);
            scheme = parse.getScheme();
        }
        return scheme == null;
    }

    /**
     * 判断是否是Url播放资源
     */
    public boolean isUrlSource() {
        String scheme = null;
        if ("vidsts".equals(PlayParameter.PLAY_PARAM_TYPE)) {
            return false;
        } else {
            Uri parse = Uri.parse(PlayParameter.PLAY_PARAM_URL_VIDEO);
            scheme = parse.getScheme();
            return scheme != null;
        }
    }

    /**
     * 播放器切换横竖屏
     */
    public void updatePlayerViewMode() {
        if (playerView != null && mContext != null) {
            int orientation = mContext.getResources().getConfiguration().orientation;
            WindowManager.LayoutParams lp =  mContext.getWindow().getAttributes();
            Window mWindow =  mContext.getWindow();
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                //LayoutParams.FLAG_FULLSCREEN 强制屏幕状态条栏弹出
                lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
                //设置属性
                mWindow.setAttributes(lp);
                //不允许窗口扩展到屏幕之外  clear掉了
                mWindow.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

                // 设置view的布局,宽高之类
                ViewGroup.LayoutParams aliVcVideoViewLayoutParams = playerView.getLayoutParams();
                aliVcVideoViewLayoutParams.height = (ScreenUtils.getWidth(mContext) * 9 / 16);
                aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                // 转到横屏,隐藏状态栏
                if (!DeviceUtil.isStrangePhone()) {
                    //获得 WindowManager.LayoutParams 属性对象
                    //直接对它flags变量操作   LayoutParams.FLAG_FULLSCREEN 表示设置全屏
                    lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    //设置属性
                    mWindow.setAttributes(lp);
                    //意思大致就是  允许窗口扩展到屏幕之外
                    mWindow.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
                }
                // 设置view的布局,宽高
                ViewGroup.LayoutParams aliVcVideoViewLayoutParams = playerView.getLayoutParams();
                aliVcVideoViewLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            }
        }
    }

    /**
     * 播放器切换横竖屏(图文详情,播放器宽高自定义)
     */
    public void updateArticlePlayerViewMode() {
        if (playerView != null && mContext != null) {
            int orientation = mContext.getResources().getConfiguration().orientation;
            WindowManager.LayoutParams lp =  mContext.getWindow().getAttributes();
            Window mWindow =  mContext.getWindow();
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                //LayoutParams.FLAG_FULLSCREEN 强制屏幕状态条栏弹出
                lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
                //设置属性
                mWindow.setAttributes(lp);
                //不允许窗口扩展到屏幕之外  clear掉了
                mWindow.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

                // 设置view的布局,宽高之类
                ViewGroup.LayoutParams aliVcVideoViewLayoutParams = playerView.getLayoutParams();
                aliVcVideoViewLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                // 转到横屏,隐藏状态栏
                if (!DeviceUtil.isStrangePhone()) {
                    //获得 WindowManager.LayoutParams 属性对象
                    //直接对它flags变量操作   LayoutParams.FLAG_FULLSCREEN 表示设置全屏
                    lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    //设置属性
                    mWindow.setAttributes(lp);
                    //意思大致就是  允许窗口扩展到屏幕之外
                    mWindow.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
                }
                // 设置view的布局,宽高
                ViewGroup.LayoutParams aliVcVideoViewLayoutParams = playerView.getLayoutParams();
                aliVcVideoViewLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                aliVcVideoViewLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            }
        }
    }

    public boolean isFullMode(){
        if (getScreenMode() == AliyunScreenMode.Full){
            return true;
        }
        return false;
    }

    private AliyunScreenMode getScreenMode() {
        if (playerView != null) {
            return playerView.getScreenMode();
        }
        return null;
    }

    /**
     * 是否在播放
     * @return
     */
    public boolean isPlaying(){
        if (playerView != null) {
            return playerView.isPlaying();
        }
        return false;
    }

    public void setScreenMode(AliyunScreenMode screenMode){
        if (playerView !=null){
            playerView.setScreenMode(screenMode);
        }
    }
    public void release(){
        if (mContext != null){
            mContext =null;
        }
        if (playerView !=null){
            playerView.onDestroy();
        }
        if (mAliyunRenderView !=null){
            mAliyunRenderView.release();
        }


    }

}