GSYVideoPlayer自定義封面
請先了解上一章節
上一章節:GSYVideoPlayer視頻播放
在播放的時候發現,直播視頻時GSY是有畫面的
但在直播音頻的時候GSY會黑屏,這樣用戶體驗感就不會很好
怎么辦?我一開始想到了三個辦法
1.放一第一幀圖片
2.放置一張背景圖
3.修改GSYVideo Player本身的背景
經過測驗我發現,方法1在播放時就不可行在加載時可展示,方法2展示了一張封面無法顯示Video控制布局
那就只能讀原始碼執行方法3了
public class LiveVideo extends StandardGSYVideoPlayer {
public LiveDataFullscreenButtonClick liveDataClick;//點擊全屏按鈕回呼
private GSYSeekBar progress;
private RelativeLayout video_parent;
/**
* 恢復暫停狀態
*/
public void onResume() {
onVideoResume();
}
/**
* 暫停狀態
*/
public void onPause() {
onVideoPause();
}
public void setBackground1(){
// this.setBackground();
}
/**
* 介面回呼
* @param liveDataClick
*/
public void setOnFullscreenButtonClick(LiveDataFullscreenButtonClick liveDataClick) {
this.liveDataClick = liveDataClick;
}
/* 重寫方法自定義layout id與video_layout_standard.xml一致 不重新使用系統默認布局*/
@Override
public int getLayoutId() {
return R.layout.livevideo_layout;
}
public LiveVideo(Context context, Boolean fullFlag) {
super(context, fullFlag);
init();
}
public LiveVideo(Context context) {
super(context);
init();
}
public LiveVideo(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void setmThumbImageView(@DrawableRes int id){
video_parent.setBackgroundResource(id);
}
/* 初始化操作 */
private void init() {
progress = findViewById(R.id.progress);
video_parent = findViewById(R.id.video_parent);
video_parent.setBackgroundResource(R.color.black);
//EXOPlayer內核,支持格式更多
// PlayerFactory.setPlayManager(Exo2PlayerManager.class);
//代理快取模式,支持所有模式,不支持m3u8等,默認
// CacheFactory.setCacheManager(ProxyCacheManager.class);
//系統內核模式
// PlayerFactory.setPlayManager(SystemPlayerManager.class);
//ijk內核,默認模式
PlayerFactory.setPlayManager(IjkPlayerManager.class);
settingsVideo();
}
/* 一些播放器的設定 做一些UI的隱藏 可根據自己需求*/
public void settingsVideo() {
GSYVideoType.enableMediaCodec();//使能硬解碼,播放前設定
Debuger.enable();//打開GSY的Log
//隱藏一些UI
setViewShowState(mBottomContainer, VISIBLE);
setViewShowState(mTopContainer, VISIBLE);
setViewShowState(mLockScreen, GONE);
setViewShowState(mLoadingProgressBar, GONE);
setViewShowState(mTopContainer, GONE);
setViewShowState(mThumbImageView, GONE);
setViewShowState(mBottomProgressBar, GONE);
//顯示一些UI 進度 時間 當前時間 全屏 回傳 加載Loading 暫停開始
setViewShowState(mStartButton, VISIBLE);
setViewShowState(mLoadingProgressBar, VISIBLE);
setViewShowState(mFullscreenButton, VISIBLE);
setViewShowState(mBackButton, GONE);
setViewShowState(mProgressBar, VISIBLE);
setViewShowState(mCurrentTimeTextView, VISIBLE);
setViewShowState(mTotalTimeTextView, VISIBLE);
setEnlargeImageRes(R.drawable.full);
setShrinkImageRes(R.drawable.full);
}
public void setTotalTime(String totalTime){
mTotalTimeTextView.setText(totalTime+"");
}
//攔截事件
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
mFullscreenButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
liveDataClick.onClick();
}
});
return super.dispatchTouchEvent(ev);
}
/* 取消 雙擊暫停 */
@Override
protected void touchDoubleUp() {
// super.touchDoubleUp();
}
public void setProgressTouch(boolean isTouch) {
progress.setTouch(isTouch);
}
public interface LiveDataFullscreenButtonClick {
void onClick();
}
}
這是我自定義的播放器繼承了StandardGSYVideoPlayer 所以需要看StandardGSYVideoPlayer的原始碼
在StandardGSYVideoPlayer類中有一個方法changeUiToPlayingShow() 就是在播放器播放時也就是
已經prepare過后會呼叫這個方法 也就是說播放時的ui都是在這里設定的
@Override
protected void changeUiToPlayingShow() {
Debuger.printfLog("changeUiToPlayingShow");
setViewShowState(mTopContainer, VISIBLE);
setViewShowState(mBottomContainer, VISIBLE);
setViewShowState(mStartButton, VISIBLE);
setViewShowState(mLoadingProgressBar, INVISIBLE);
//可以看到隱藏了這個Layout,這個就是封面的Layout 所以修改這個就可以了
/*
在自定義的播放器中重寫這個方法 然后對這個進行設定
*/
setViewShowState(mThumbImageViewLayout, INVISIBLE);
setViewShowState(mBottomProgressBar, INVISIBLE);
setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
if (mLoadingProgressBar instanceof ENDownloadView) {
((ENDownloadView) mLoadingProgressBar).reset();
}
updateStartImage();
}
測驗一下發現圖片時有展示的,但是有個BUG我們的封面展示后,無法點擊展示我們的全屏按鈕播放按鈕布局了
這時我們需要對封面這個Layout進行動態的Ontouch監聽
mThumbImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*
* 當觸碰時 就根據需求顯示對應的View
* 比如全屏按鈕 進度條 開始暫停按鈕等
* */
}
});
還有一些其他狀態的方法 根據需求進行自定義修改
@Override
protected void changeUiToNormal() {
Debuger.printfLog("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();
if (mLoadingProgressBar instanceof ENDownloadView) {
((ENDownloadView) mLoadingProgressBar).reset();
}
}
@Override
protected void changeUiToPreparingShow() {
Debuger.printfLog("changeUiToPreparingShow");
setViewShowState(mTopContainer, VISIBLE);
setViewShowState(mBottomContainer, VISIBLE);
setViewShowState(mStartButton, INVISIBLE);
setViewShowState(mLoadingProgressBar, VISIBLE);
setViewShowState(mThumbImageViewLayout, INVISIBLE);
setViewShowState(mBottomProgressBar, INVISIBLE);
setViewShowState(mLockScreen, GONE);
if (mLoadingProgressBar instanceof ENDownloadView) {
ENDownloadView enDownloadView = (ENDownloadView) mLoadingProgressBar;
if (enDownloadView.getCurrentState() == ENDownloadView.STATE_PRE) {
((ENDownloadView) mLoadingProgressBar).start();
}
}
}
@Override
protected void changeUiToPlayingShow() {
Debuger.printfLog("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);
if (mLoadingProgressBar instanceof ENDownloadView) {
((ENDownloadView) mLoadingProgressBar).reset();
}
updateStartImage();
}
@Override
protected void changeUiToPauseShow() {
Debuger.printfLog("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);
if (mLoadingProgressBar instanceof ENDownloadView) {
((ENDownloadView) mLoadingProgressBar).reset();
}
updateStartImage();
updatePauseCover();
}
@Override
protected void changeUiToPlayingBufferingShow() {
Debuger.printfLog("changeUiToPlayingBufferingShow");
setViewShowState(mTopContainer, VISIBLE);
setViewShowState(mBottomContainer, VISIBLE);
setViewShowState(mStartButton, INVISIBLE);
setViewShowState(mLoadingProgressBar, VISIBLE);
setViewShowState(mThumbImageViewLayout, INVISIBLE);
setViewShowState(mBottomProgressBar, INVISIBLE);
setViewShowState(mLockScreen, GONE);
if (mLoadingProgressBar instanceof ENDownloadView) {
ENDownloadView enDownloadView = (ENDownloadView) mLoadingProgressBar;
if (enDownloadView.getCurrentState() == ENDownloadView.STATE_PRE) {
((ENDownloadView) mLoadingProgressBar).start();
}
}
}
@Override
protected void changeUiToCompleteShow() {
Debuger.printfLog("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);
if (mLoadingProgressBar instanceof ENDownloadView) {
((ENDownloadView) mLoadingProgressBar).reset();
}
updateStartImage();
}
@Override
protected void changeUiToError() {
Debuger.printfLog("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);
if (mLoadingProgressBar instanceof ENDownloadView) {
((ENDownloadView) mLoadingProgressBar).reset();
}
updateStartImage();
}
布局操作進行修改
我們在布局中有創建一些id和原GSYVideo一致的Layout
在原始碼中看到對最外層的RelativeLayout沒有操作
也就是說我們可以動態修改最外層的background進行展示同時不會影響播放的操作
這是一種比較穩妥的方法不會修改原始碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/video_parent"
android:background="@color/black">
<RelativeLayout
android:id="@+id/surface_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</RelativeLayout>
<RelativeLayout
android:id="@+id/thumb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#000000"
android:scaleType="fitCenter" />
<LinearLayout
android:id="@+id/layout_bottom"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#99000000"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="invisible">
<TextView
android:id="@+id/current"
android:textColor="@color/white"
style="@style/news_des_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="00:00"
/>
<com.blue.bCheng.views.GSYSeekBar
android:id="@+id/progress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
android:background="@null"
android:padding="10dp"
android:max="100"
android:maxHeight="4dp"
android:minHeight="4dp"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:progressDrawable="@drawable/video_progress_bg"
android:thumb="@drawable/video_seek_thumb" />
<TextView
android:id="@+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="00:00"
android:textColor="@color/white"
style="@style/news_des_style"/>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_top"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/video_title_bg"
android:gravity="center_vertical">
<ImageView
android:id="@+id/back"
android:layout_width="48dp"
android:layout_height="48dp"
android:paddingLeft="10dp"
android:scaleType="centerInside"
android:src="@drawable/video_back" />
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<ImageView
android:id="@+id/fullscreen"
style="@style/left_icon_style"
android:src="@drawable/full" />
</LinearLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loading"
android:layout_centerInParent="true"
/>
<ImageView
android:visibility="gone"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:padding="5dp"
android:id="@+id/start"
/>
</RelativeLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/279287.html
標籤:其他
