GlVideoPlayerView.java 11 KB
package com.glsx.commonres.player;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.glsx.commonres.R;
import com.glsx.commonres.player.widget.GlVideoView;
import com.glsx.commonres.util.FlowCardOverdueStateCallback;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.player.IjkPlayerManager;
import com.shuyu.gsyvideoplayer.player.PlayerFactory;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;

import tv.danmaku.ijk.media.exo2.Exo2PlayerManager;

/**
 * 自定义视频播放器View
 */
public class GlVideoPlayerView extends RelativeLayout implements GlVideoView.RemoteVideoListener {
    private Context mContext;
    private GlVideoPlayerListener mListener;
    private GlVideoView videoView;
    private OrientationUtils orientationUtils;

    public interface GlVideoPlayerListener {

        void onClickStartBtn();

        void onPlayerPrepared();

        void onPlayerPause();

        void onPlayerStop();

        void onPlayerCacheTimeOut();

        void onClickSwitchCamera();
    }

    public GlVideoPlayerView(Context context) {
        super(context);
        initView(context);
    }

    public GlVideoPlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public GlVideoPlayerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.public_layout_video_player, this);
        videoView = findViewById(R.id.gl_video_view);
//        switch2Exo();
        orientationUtils = new OrientationUtils((Activity) context, videoView);
        //初始化不打开外部的旋转
        orientationUtils.setEnable(false);
        videoView.initParams(context);
        videoView.registerListener(this);
    }

    public int getCurrentState() {
        return videoView.getCurrentState();
    }

    /**
     * 设置缩略图控件
     *
     * @param thumbView 缩略图控件
     */
    public void setThumbImageView(ImageView thumbView) {
        videoView.setThumbImageView(thumbView);
    }

    /**
     * 设置是否显示录像状态UI
     *
     * @param isRecording 是否录像状态.回看页面为非录像状态
     */
    public void setRecordState(boolean isRecording) {
        videoView.setRecordState(isRecording);
    }

    /**
     * 是否显示全屏按钮
     *
     * @param isShow 是否显示
     */
    public void setShowFullscreenBtn(boolean isShow) {
        videoView.setShowFullscreenBtn(isShow);
    }

    /**
     * 设置全屏按钮点击监听
     * @param listener
     */
    public void setFullScreenBtnClickListener(View.OnClickListener listener) {
        if (null != listener) {
            videoView.setFullScreenBtnClickListener(listener);
        }
    }

    /**
     * 设置显示视频标题
     * @param isShow
     */
    public void setShowTitle(boolean isShow) {
        videoView.setShowTitle(isShow);
    }

    /**
     * 设置显示返回按钮
     * @param isShow
     */
    public void setShowBackBtn(boolean isShow) {
        videoView.setShowBackBtn(isShow);
    }

    /**
     * 设置是否可以滑动界面改变进度,声音等
     * @param isTouchWidget
     */
    public void setIsTouchWidget(boolean isTouchWidget) {
        videoView.setIsTouchWidget(isTouchWidget);
    }

    public void pausePlay() {
        videoView.pausePlay();
    }

    public boolean isPlaying() {
        return videoView.isPlaying();
    }

    public void seekTo(int progress) {
        videoView.seekTo(progress);
    }

    /**
     * 设置播放URL,并立即播放
     *
     * @param url
     */
    public void setNormalUrlAndPlayNow(String url) {
//        videoView.setNormalUrlAndPlayNow(url);
        if (!TextUtils.isEmpty(url)) {
            getVideoOptionBuilder(url).build(videoView);
            videoView.setLiveStreamUrlAndPlayNow(url);
        }
    }

    /**
     * 设置播放URL,点击播放按钮才开始播放
     *
     * @param url
     */
    public void setNormalUrlAndClickPlay(String url) {
        if (!TextUtils.isEmpty(url)) {
            getVideoOptionBuilder(url).build(videoView);
            videoView.setNormalUrlAndClickPlay(url);
        }
    }

    /**
     * 设置流播放连接,点击开始播放按钮请求直播流.
     *
     * @param url 流连接
     */
    public void setLiveStreamUrlAndPlayNow(String url) {
        if (!TextUtils.isEmpty(url)) {
            getLiveVideoOptionBuilder(url).build(videoView);
            videoView.setLiveStreamUrlAndPlayNow(url);
        }
    }

    private GSYVideoOptionBuilder getLiveVideoOptionBuilder(String url) {
        GSYVideoOptionBuilder gsyVideoOption = new GSYVideoOptionBuilder();
//        gsyVideoOption.setAutoFullWithSize(true);
        //是否使用全屏动画效果
        gsyVideoOption.setShowFullAnimation(false);
        //是否可以滑动界面改变进度,声音等
        gsyVideoOption.setIsTouchWiget(false);
        //是否可以全屏滑动界面改变进度,声音等
        gsyVideoOption.setIsTouchWigetFull(false);
        //一全屏就锁屏横屏,默认false竖屏,可配合setRotateViewAuto使用
        gsyVideoOption.setLockLand(true);
        //是否边缓存,m3u8等无效
        gsyVideoOption.setCacheWithPlay(false);
        //播放url
        gsyVideoOption.setUrl(url);
        return gsyVideoOption;
    }

    private GSYVideoOptionBuilder getVideoOptionBuilder(String url) {
        GSYVideoOptionBuilder gsyVideoOption = new GSYVideoOptionBuilder();
//        gsyVideoOption.setAutoFullWithSize(true);
        //是否使用全屏动画效果
        gsyVideoOption.setShowFullAnimation(false);
        //是否可以滑动界面改变进度,声音等
        gsyVideoOption.setIsTouchWiget(false);
        //是否可以全屏滑动界面改变进度,声音等
        gsyVideoOption.setIsTouchWigetFull(false);
        //一全屏就锁屏横屏,默认false竖屏,可配合setRotateViewAuto使用
        gsyVideoOption.setLockLand(true);
        //是否边缓存,m3u8等无效
        gsyVideoOption.setCacheWithPlay(true);
        //播放url
        gsyVideoOption.setUrl(url);
        return gsyVideoOption;
    }

    /**
     * 点击返回按钮先退出全屏模式
     *
     * @param activity Activity的Context对象
     * @return true:退出全屏模式,false可以直接退出.
     */
    public boolean onVideoBack(Context activity) {
        if (orientationUtils != null) {
            orientationUtils.backToProtVideo();
        }
        return videoView.onVideoBack(activity);
    }

    /**
     * 停止播放
     */
    public void stopPlay() {
        videoView.stopPlay();
    }

    /**
     * 界面退出,销毁播放器资源
     */
    public void onDestroyView() {
        videoView.onDestroyView(this);
        if (orientationUtils != null)
            orientationUtils.releaseListener();
        orientationUtils = null;
    }

    /**
     * 注册播放状态回调监听,需要获取相应状态的页面需要注册
     *
     * @param listener 监听回调对象
     */
    public void registerPlayerListener(GlVideoPlayerListener listener) {
        mListener = listener;
    }

    /**
     * 注销播放状态回调监听,需要获取相应状态的页面需要注册
     */
    public void unregisterListener() {
        mListener = null;
    }

    @Override
    public void onClickStartBtn() {
        if (null != mListener) {
            mListener.onClickStartBtn();
        }
    }

    @Override
    public void onPlayerPrepared() {
        if (null != mListener) {
            mListener.onPlayerPrepared();
        }
    }

    @Override
    public void onPlayerPause() {
        if (null != mListener) {
            mListener.onPlayerPause();
        }
    }

    @Override
    public void onPlayerStop() {
        if (null != mListener) {
            mListener.onPlayerStop();
        }
    }

    @Override
    public void onPlayerCacheTimeOut() {
        if (null != mListener) {
            mListener.onPlayerCacheTimeOut();
        }
    }

    @Override
    public void onClickSwitchCamera() {
        if (null != mListener) {
            mListener.onClickSwitchCamera();
        }
    }

    public void setPlayTag(String id) {
        videoView.setPlayTag(id);
    }

    public void setPlayPosition(int playPosition) {
        videoView.setPlayPosition(playPosition);
    }

    public void setReleaseWhenLossAudio(boolean state) {
        videoView.setReleaseWhenLossAudio(state);
    }

    /**
     * 加载视频第一帧作为封面
     *
     * @param context context
     * @param url     视频URL
     */
    public void loadCover(Context context, String url) {
        ImageView imageView = new ImageView(context);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        imageView.setLayoutParams(layoutParams);
//        Glide.with(context)
//                .load(url)
//                .centerCrop()
//                .error(R.drawable.icon_travel_video_cover_defult)
//                .placeholder(R.drawable.icon_travel_video_cover_defult)
//                .into(imageView);
        setThumbImageView(imageView);
    }

    public void setTimeout(int timeOut) {
        videoView.setTimeout(timeOut);
    }

    public void onPlayError(int what, int extra){
        videoView.onError(what, extra);
    }

    public static void switch2Exo2PlayerManager() {
        //EXOPlayer内核,支持FFmpeg生成的视频格式播放  避免ffmpeg编码视频出现的有声音无画面情况
//        PlayerFactory.setPlayManager(Exo2PlayerManager.class);
    }

    public static void switch2IjkPlayerManager() {
//        PlayerFactory.setPlayManager(IjkPlayerManager.class);
    }

    public void switch2Exo() {
        PlayerFactory.setPlayManager(Exo2PlayerManager.class);
    }

    public void switch2Ijk() {
        PlayerFactory.setPlayManager(IjkPlayerManager.class);
    }

    public void showHideSwitchCamera(boolean isShow) {
        videoView.showHideSwitchCamera(isShow);
    }

    public void updateCameraDirIcon(boolean isFront) {
        videoView.updateCameraDirIcon(isFront);
    }

    public void resetVideoOptionModel() {
        if (videoView != null) {
            videoView.resetVideoOptionModel();
        }
    }

    public void setFlowCardOverdueState(boolean isOverdue, FlowCardOverdueStateCallback callback) {
        if (videoView != null) {
            videoView.updateOverdueState(isOverdue, callback);
        }
    }

    public void remoteLiveFullscreen() {
        if (videoView != null) {
            videoView.remoteLiveFullscreen();
        }
    }

}