package com.lqr.biliblili.mvp.ui.widget.player; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.lqr.biliblili.R; import com.shuyu.gsyvideoplayer.listener.GSYVideoShotListener; import com.shuyu.gsyvideoplayer.listener.GSYVideoShotSaveListener; import com.shuyu.gsyvideoplayer.listener.StandardVideoAllCallBack; import com.shuyu.gsyvideoplayer.utils.CommonUtil; import com.shuyu.gsyvideoplayer.utils.NetworkUtils; import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer; import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer; import java.io.File; /** * @创建者 CSDN_LQR * @描述 自定义仿Bilibili播放器 */ public class LQRBiliPlayer extends GSYVideoPlayer { protected StandardVideoAllCallBack mStandardVideoAllCallBack; // 亮度dialog protected Dialog mBrightnessDialog; // 音量dialog protected Dialog mVolumeDialog; // 触摸进度dialog protected Dialog mProgressDialog; // 音量进度条的progress protected ProgressBar mDialogVolumeProgressBar; // 亮度进度条的progress protected ProgressBar mDialogBrightnessProgressBar; // 音量文本 protected TextView mVolumeDialogTv; // 亮度文本 protected TextView mBrightnessDialogTv; // 触摸移动显示文本 protected TextView mDialogSeekTime; // 触摸移动显示全部时间 protected TextView mDialogTotalTime; // 触摸移动显示时间差(+xx秒 或 -xx秒) protected TextView mDialogDiffTime; protected Drawable mBottomProgressDrawable; protected Drawable mBottomShowProgressDrawable; protected Drawable mBottomShowProgressThumbDrawable; protected Drawable mVolumeProgressDrawable; protected Drawable mBrightnessProgressDrawable; // protected Drawable mDialogProgressBarDrawable; protected int mDialogProgressHighLightColor = -11; protected int mDialogProgressNormalColor = -11; private ImageView mIvStartStop; public LQRBiliPlayer(Context context, Boolean fullFlag) { super(context, fullFlag); } public LQRBiliPlayer(Context context) { super(context); } public LQRBiliPlayer(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void init(Context context) { super.init(context); // 增加自定义ui mIvStartStop = findViewById(R.id.start_stop); mIvStartStop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mStartButton.performClick(); } }); if (mBottomProgressDrawable != null) { mBottomProgressBar.setProgressDrawable(mBottomProgressDrawable); } if (mBottomShowProgressDrawable != null) { mProgressBar.setProgressDrawable(mBottomProgressDrawable); } if (mBottomShowProgressThumbDrawable != null) { mProgressBar.setThumb(mBottomShowProgressThumbDrawable); } } /** * 继承后重写可替换为你需要的布局 */ @Override public int getLayoutId() { return R.layout.widget_lqr_bilibili_player; } @Override public void startPlayLogic() { if (mStandardVideoAllCallBack != null) { mStandardVideoAllCallBack.onClickStartThumb(mOriginUrl, mTitle, LQRBiliPlayer.this); } prepareVideo(); startDismissControlViewTimer(); } /** * 显示wifi确定框,如需要自定义继承重写即可 */ @Override protected void showWifiDialog() { if (!NetworkUtils.isAvailable(mContext)) { Toast.makeText(mContext, getResources().getString(com.shuyu.gsyvideoplayer.R.string.no_net), Toast.LENGTH_SHORT).show(); return; } AlertDialog.Builder builder = new AlertDialog.Builder(getActivityContext()); builder.setMessage(getResources().getString(com.shuyu.gsyvideoplayer.R.string.tips_not_wifi)); builder.setPositiveButton(getResources().getString(R.string.tips_not_wifi_confirm), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); startPlayLogic(); } }); builder.setNegativeButton(getResources().getString(R.string.tips_not_wifi_cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } /** * 触摸显示滑动进度dialog,如需要自定义继承重写即可,记得重写dismissProgressDialog */ @Override protected void showProgressDialog(float deltaX, String seekTime, int seekTimePosition, String totalTime, int totalTimeDuration) { if (mProgressDialog == null) { View localView = LayoutInflater.from(getActivityContext()).inflate(R.layout.widget_lqr_bilibili_video_progress_dialog, null); mDialogSeekTime = localView.findViewById(R.id.tv_current); mDialogTotalTime = localView.findViewById(R.id.tv_duration); mDialogDiffTime = localView.findViewById(R.id.tv_diff); mProgressDialog = new Dialog(getActivityContext(), R.style.video_style_dialog_progress); mProgressDialog.setContentView(localView); mProgressDialog.getWindow().addFlags(Window.FEATURE_ACTION_BAR); mProgressDialog.getWindow().addFlags(32); mProgressDialog.getWindow().addFlags(16); mProgressDialog.getWindow().setLayout(getWidth(), getHeight()); if (mDialogProgressNormalColor != -11) { mDialogTotalTime.setTextColor(mDialogProgressNormalColor); } if (mDialogProgressHighLightColor != -11) { mDialogSeekTime.setTextColor(mDialogProgressHighLightColor); } WindowManager.LayoutParams localLayoutParams = mProgressDialog.getWindow().getAttributes(); localLayoutParams.gravity = Gravity.TOP; localLayoutParams.width = getWidth(); localLayoutParams.height = getHeight(); int location[] = new int[2]; getLocationOnScreen(location); localLayoutParams.x = location[0]; localLayoutParams.y = location[1]; mProgressDialog.getWindow().setAttributes(localLayoutParams); } if (!mProgressDialog.isShowing()) { mProgressDialog.show(); } mDialogSeekTime.setText(seekTime); mDialogTotalTime.setText(" / " + totalTime); // TODO: 2017/11/1 滑动进度的差值需要计算 int curWidth = CommonUtil.getCurrentScreenLand((Activity) getActivityContext()) ? mScreenHeight : mScreenWidth; mDialogDiffTime.setText(CommonUtil.stringForTime((int) (deltaX * totalTimeDuration / curWidth / mSeekRatio)) + "秒"); } @Override protected void dismissProgressDialog() { if (mProgressDialog != null) { mProgressDialog.dismiss(); mProgressDialog = null; } } /** * 触摸音量dialog,如需要自定义继承重写即可,记得重写dismissVolumeDialog */ @Override protected void showVolumeDialog(float deltaY, int volumePercent) { if (mVolumeDialog == null) { View localView = LayoutInflater.from(getActivityContext()).inflate(R.layout.widget_lqr_bilibili_video_volume_dialog, null); mVolumeDialogTv = (TextView) localView.findViewById(R.id.app_video_volume); mDialogVolumeProgressBar = ((ProgressBar) localView.findViewById(R.id.volume_progressbar)); if (mVolumeProgressDrawable != null) { mDialogBrightnessProgressBar.setProgressDrawable(mVolumeProgressDrawable); } mVolumeDialog = new Dialog(getActivityContext(), R.style.video_style_dialog_progress); mVolumeDialog.setContentView(localView); mVolumeDialog.getWindow().addFlags(8); mVolumeDialog.getWindow().addFlags(32); mVolumeDialog.getWindow().addFlags(16); mVolumeDialog.getWindow().setLayout(-2, -2); WindowManager.LayoutParams localLayoutParams = mVolumeDialog.getWindow().getAttributes(); localLayoutParams.gravity = Gravity.TOP | Gravity.LEFT; localLayoutParams.width = getWidth(); localLayoutParams.height = getHeight(); int location[] = new int[2]; getLocationOnScreen(location); localLayoutParams.x = location[0]; localLayoutParams.y = location[1]; mVolumeDialog.getWindow().setAttributes(localLayoutParams); } if (!mVolumeDialog.isShowing()) { mVolumeDialog.show(); } mDialogVolumeProgressBar.setProgress(volumePercent); if (mBrightnessDialogTv != null) mBrightnessDialogTv.setText((int) (volumePercent * 100) + "%"); } @Override protected void dismissVolumeDialog() { if (mVolumeDialog != null) { mVolumeDialog.dismiss(); mVolumeDialog = null; } } /** * 触摸亮度dialog,如需要自定义继承重写即可,记得重写dismissBrightnessDialog */ @Override protected void showBrightnessDialog(float percent) { if (mBrightnessDialog == null) { View localView = LayoutInflater.from(getActivityContext()).inflate(R.layout.widget_lqr_bilibili_video_brightness_dialog, null); mBrightnessDialogTv = (TextView) localView.findViewById(R.id.app_video_brightness); mDialogBrightnessProgressBar = ((ProgressBar) localView.findViewById(R.id.brightness_progressbar)); if (mBrightnessProgressDrawable != null) { mDialogVolumeProgressBar.setProgressDrawable(mBrightnessProgressDrawable); } mBrightnessDialog = new Dialog(getActivityContext(), R.style.video_style_dialog_progress); mBrightnessDialog.setContentView(localView); mBrightnessDialog.getWindow().addFlags(8); mBrightnessDialog.getWindow().addFlags(32); mBrightnessDialog.getWindow().addFlags(16); mBrightnessDialog.getWindow().setLayout(-2, -2); WindowManager.LayoutParams localLayoutParams = mBrightnessDialog.getWindow().getAttributes(); localLayoutParams.gravity = Gravity.TOP | Gravity.RIGHT; localLayoutParams.width = getWidth(); localLayoutParams.height = getHeight(); int location[] = new int[2]; getLocationOnScreen(location); localLayoutParams.x = location[0]; localLayoutParams.y = location[1]; mBrightnessDialog.getWindow().setAttributes(localLayoutParams); } if (!mBrightnessDialog.isShowing()) { mBrightnessDialog.show(); } mDialogBrightnessProgressBar.setProgress((int) percent); if (mBrightnessDialogTv != null) mBrightnessDialogTv.setText((int) (percent * 100) + "%"); } @Override protected void dismissBrightnessDialog() { if (mBrightnessDialog != null) { mBrightnessDialog.dismiss(); mBrightnessDialog = null; } } /** * 将自定义的效果也设置到全屏 * * @param context * @param actionBar 是否有actionBar,有的话需要隐藏 * @param statusBar 是否有状态bar,有的话需要隐藏 * @return */ @Override public GSYBaseVideoPlayer startWindowFullscreen(Context context, boolean actionBar, boolean statusBar) { GSYBaseVideoPlayer gsyBaseVideoPlayer = super.startWindowFullscreen(context, actionBar, statusBar); if (gsyBaseVideoPlayer != null) { LQRBiliPlayer gsyVideoPlayer = (LQRBiliPlayer) gsyBaseVideoPlayer; gsyVideoPlayer.setStandardVideoAllCallBack(mStandardVideoAllCallBack); gsyVideoPlayer.setLockClickListener(mLockClickListener); gsyVideoPlayer.setNeedLockFull(isNeedLockFull()); initFullUI(gsyVideoPlayer); //比如你自定义了返回案件,但是因为返回按键底层已经设置了返回事件,所以你需要在这里重新增加的逻辑 } return gsyBaseVideoPlayer; } /** * 点击触摸显示和隐藏逻辑 */ @Override protected void onClickUiToggle() { if (mIfCurrentIsFullscreen && mLockCurScreen && mNeedLockFull) { setViewShowState(mLockScreen, VISIBLE); return; } if (mCurrentState == CURRENT_STATE_PREPAREING) { if (mBottomContainer != null) { if (mBottomContainer.getVisibility() == View.VISIBLE) { changeUiToPrepareingClear(); } else { changeUiToPreparingShow(); } } } else if (mCurrentState == CURRENT_STATE_PLAYING) { if (mBottomContainer != null) { if (mBottomContainer.getVisibility() == View.VISIBLE) { changeUiToPlayingClear(); } else { changeUiToPlayingShow(); } } } else if (mCurrentState == CURRENT_STATE_PAUSE) { if (mBottomContainer != null) { if (mBottomContainer.getVisibility() == View.VISIBLE) { changeUiToPauseClear(); } else { changeUiToPauseShow(); } } } else if (mCurrentState == CURRENT_STATE_AUTO_COMPLETE) { if (mBottomContainer != null) { if (mBottomContainer.getVisibility() == View.VISIBLE) { changeUiToCompleteClear(); } else { changeUiToCompleteShow(); } } } else if (mCurrentState == CURRENT_STATE_PLAYING_BUFFERING_START) { if (mBottomContainer != null) { if (mBottomContainer.getVisibility() == View.VISIBLE) { changeUiToPlayingBufferingClear(); } else { changeUiToPlayingBufferingShow(); } } } // 控件显隐监听 if (mCurrentState == CURRENT_STATE_PLAYING) { if (mBottomContainer != null) { if (mBottomContainer.getVisibility() == View.VISIBLE) { if (mOnWidgetVisibleListener != null) { mOnWidgetVisibleListener.onShow(); } } else { if (mOnWidgetVisibleListener != null) { mOnWidgetVisibleListener.onHide(); } } } } else { if (mOnWidgetVisibleListener != null) { mOnWidgetVisibleListener.onShow(); } } } @Override protected void hideAllWidget() { setViewShowState(mBottomContainer, INVISIBLE); setViewShowState(mTopContainer, INVISIBLE); setViewShowState(mBottomProgressBar, VISIBLE); setViewShowState(mStartButton, INVISIBLE); if (mOnWidgetVisibleListener != null) { mOnWidgetVisibleListener.onHide(); } } @Override protected void changeUiToNormal() { setViewShowState(mTopContainer, VISIBLE); setViewShowState(mBottomContainer, INVISIBLE); setViewShowState(mStartButton, VISIBLE); setViewShowState(mLoadingProgressBar, INVISIBLE); setViewShowState(mThumbImageViewLayout, VISIBLE); setViewShowState(mBottomProgressBar, INVISIBLE); setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE); updateStartImage(); } protected void changeUiToClear() { setViewShowState(mTopContainer, INVISIBLE); setViewShowState(mBottomContainer, INVISIBLE); setViewShowState(mStartButton, INVISIBLE); setViewShowState(mLoadingProgressBar, INVISIBLE); setViewShowState(mThumbImageViewLayout, INVISIBLE); setViewShowState(mBottomProgressBar, INVISIBLE); setViewShowState(mLockScreen, GONE); } protected void changeUiToPrepareingClear() { setViewShowState(mTopContainer, INVISIBLE); setViewShowState(mBottomContainer, INVISIBLE); setViewShowState(mStartButton, INVISIBLE); setViewShowState(mLoadingProgressBar, INVISIBLE); setViewShowState(mThumbImageViewLayout, INVISIBLE); setViewShowState(mBottomProgressBar, INVISIBLE); setViewShowState(mLockScreen, GONE); } @Override protected void changeUiToPreparingShow() { setViewShowState(mTopContainer, VISIBLE); setViewShowState(mBottomContainer, VISIBLE); setViewShowState(mStartButton, INVISIBLE); setViewShowState(mLoadingProgressBar, VISIBLE); setViewShowState(mThumbImageViewLayout, INVISIBLE); setViewShowState(mBottomProgressBar, INVISIBLE); setViewShowState(mLockScreen, GONE); } protected void changeUiToPlayingClear() { changeUiToClear(); setViewShowState(mBottomProgressBar, VISIBLE); } @Override protected void changeUiToPlayingShow() { setViewShowState(mTopContainer, VISIBLE); setViewShowState(mBottomContainer, VISIBLE); setViewShowState(mStartButton, VISIBLE); setViewShowState(mLoadingProgressBar, INVISIBLE); setViewShowState(mThumbImageViewLayout, INVISIBLE); setViewShowState(mBottomProgressBar, INVISIBLE); setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE); updateStartImage(); } protected void changeUiToPauseClear() { changeUiToClear(); setViewShowState(mBottomProgressBar, VISIBLE); updatePauseCover(); } @Override protected void changeUiToPauseShow() { setViewShowState(mTopContainer, VISIBLE); setViewShowState(mBottomContainer, VISIBLE); setViewShowState(mStartButton, VISIBLE); setViewShowState(mLoadingProgressBar, INVISIBLE); setViewShowState(mThumbImageViewLayout, INVISIBLE); setViewShowState(mBottomProgressBar, INVISIBLE); setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE); updateStartImage(); updatePauseCover(); } @Override protected void changeUiToError() { setViewShowState(mTopContainer, INVISIBLE); setViewShowState(mBottomContainer, INVISIBLE); setViewShowState(mStartButton, VISIBLE); setViewShowState(mLoadingProgressBar, INVISIBLE); setViewShowState(mThumbImageViewLayout, INVISIBLE); setViewShowState(mBottomProgressBar, INVISIBLE); setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE); updateStartImage(); } protected void changeUiToCompleteClear() { setViewShowState(mTopContainer, INVISIBLE); setViewShowState(mBottomContainer, INVISIBLE); setViewShowState(mStartButton, VISIBLE); setViewShowState(mLoadingProgressBar, INVISIBLE); setViewShowState(mThumbImageViewLayout, VISIBLE); setViewShowState(mBottomProgressBar, VISIBLE); setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE); updateStartImage(); } @Override protected void changeUiToCompleteShow() { setViewShowState(mTopContainer, VISIBLE); setViewShowState(mBottomContainer, VISIBLE); setViewShowState(mStartButton, VISIBLE); setViewShowState(mLoadingProgressBar, INVISIBLE); setViewShowState(mThumbImageViewLayout, VISIBLE); setViewShowState(mBottomProgressBar, INVISIBLE); setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE); updateStartImage(); } protected void changeUiToPlayingBufferingClear() { setViewShowState(mTopContainer, INVISIBLE); setViewShowState(mBottomContainer, INVISIBLE); setViewShowState(mStartButton, INVISIBLE); setViewShowState(mLoadingProgressBar, VISIBLE); setViewShowState(mThumbImageViewLayout, INVISIBLE); setViewShowState(mBottomProgressBar, VISIBLE); setViewShowState(mLockScreen, GONE); updateStartImage(); } @Override protected void changeUiToPlayingBufferingShow() { setViewShowState(mTopContainer, VISIBLE); setViewShowState(mBottomContainer, VISIBLE); setViewShowState(mStartButton, INVISIBLE); setViewShowState(mLoadingProgressBar, VISIBLE); setViewShowState(mThumbImageViewLayout, INVISIBLE); setViewShowState(mBottomProgressBar, INVISIBLE); setViewShowState(mLockScreen, GONE); } /** * 定义开始按键显示 */ protected void updateStartImage() { if (mStartButton instanceof ImageView) { ImageView imageView = (ImageView) mStartButton; if (mCurrentState == CURRENT_STATE_PLAYING) { imageView.setImageResource(R.mipmap.ic_tv_stop); mIvStartStop.setImageResource(R.mipmap.bili_player_play_can_pause); } else if (mCurrentState == CURRENT_STATE_ERROR) { imageView.setImageResource(R.mipmap.ic_tv_play); mIvStartStop.setImageResource(R.mipmap.bili_player_play_can_play); } else { imageView.setImageResource(R.mipmap.ic_tv_play); mIvStartStop.setImageResource(R.mipmap.bili_player_play_can_play); } } } /** * 全屏的UI逻辑 */ private void initFullUI(LQRBiliPlayer LQRBiliBiliPlayer) { if (mBottomProgressDrawable != null) { LQRBiliBiliPlayer.setBottomProgressBarDrawable(mBottomProgressDrawable); } if (mBottomShowProgressDrawable != null && mBottomShowProgressThumbDrawable != null) { LQRBiliBiliPlayer.setBottomShowProgressBarDrawable(mBottomShowProgressDrawable, mBottomShowProgressThumbDrawable); } if (mVolumeProgressDrawable != null) { LQRBiliBiliPlayer.setDialogVolumeProgressBar(mVolumeProgressDrawable); } if (mBrightnessProgressDrawable != null) { LQRBiliBiliPlayer.setBrightnessProgressDrawable(mBrightnessProgressDrawable); } } /** * 底部进度条-弹出的 */ public void setBottomShowProgressBarDrawable(Drawable drawable, Drawable thumb) { mBottomShowProgressDrawable = drawable; mBottomShowProgressThumbDrawable = thumb; if (mProgressBar != null) { mProgressBar.setProgressDrawable(drawable); mProgressBar.setThumb(thumb); } } /** * 底部进度条-非弹出 */ public void setBottomProgressBarDrawable(Drawable drawable) { mBottomProgressDrawable = drawable; if (mBottomProgressBar != null) { mBottomProgressBar.setProgressDrawable(drawable); } } /** * 声音进度条 */ public void setDialogVolumeProgressBar(Drawable drawable) { mVolumeProgressDrawable = drawable; } /** * 亮度进度条 */ public void setBrightnessProgressDrawable(Drawable drawable) { mBrightnessProgressDrawable = drawable; } /** * 中间进度条字体颜色 */ public void setDialogProgressColor(int highLightColor, int normalColor) { mDialogProgressHighLightColor = highLightColor; mDialogProgressNormalColor = normalColor; } public void setStandardVideoAllCallBack(StandardVideoAllCallBack standardVideoAllCallBack) { this.mStandardVideoAllCallBack = standardVideoAllCallBack; setVideoAllCallBack(standardVideoAllCallBack); } /************************************* 关于截图的 ****************************************/ /** * 获取截图 */ public void taskShotPic(GSYVideoShotListener gsyVideoShotListener) { this.taskShotPic(gsyVideoShotListener, false); } /** * 获取截图 * * @param high 是否需要高清的 */ public void taskShotPic(GSYVideoShotListener gsyVideoShotListener, boolean high) { if (getCurrentPlayer().getRenderProxy() != null) { getCurrentPlayer().getRenderProxy().taskShotPic(gsyVideoShotListener, high); } } /** * 保存截图 */ public void saveFrame(final File file, GSYVideoShotSaveListener gsyVideoShotSaveListener) { saveFrame(file, false, gsyVideoShotSaveListener); } /** * 保存截图 * * @param high 是否需要高清的 */ public void saveFrame(final File file, final boolean high, final GSYVideoShotSaveListener gsyVideoShotSaveListener) { if (getCurrentPlayer().getRenderProxy() != null) { getCurrentPlayer().getRenderProxy().saveFrame(file, high, gsyVideoShotSaveListener); } } /************************************* 控件显隐监听 ****************************************/ OnWidgetVisibleListener mOnWidgetVisibleListener; public OnWidgetVisibleListener getOnWidgetVisibleListener() { return mOnWidgetVisibleListener; } public void setOnWidgetVisibleListener(OnWidgetVisibleListener onWidgetVisibleListener) { mOnWidgetVisibleListener = onWidgetVisibleListener; } public interface OnWidgetVisibleListener { void onShow(); void onHide(); } }