強大的第三方視訊播放器。
近來靜極思動,決定寫一堆部落格。
沒別的意思,單純的想裝個逼。
//匯入依賴 餃子播放器 https://github.com/lipangit/JiaoZiVideoPlayer
//視訊播放器
compile 'cn.jzvd:jiaozivideoplayer:6.2.9'
//為了能夠帶入到專案中使用 需要自定播放器
public class MyJZVideoPlayerStandard extends JZVideoPlayerStandard { private Context mContext; public MyJZVideoPlayerStandard(Context context) { super(context); mContext=context; } public MyJZVideoPlayerStandard(Context context, AttributeSet attrs) { super(context, attrs); mContext=context; } @Override public void init(Context context) { super.init(context); mContext=context; } @Override public void onClick(View v) { super.onClick(v); } @Override public int getLayoutId() { return cn.jzvd.R.layout.jz_layout_standard; } @Override public boolean onTouch(View v, MotionEvent event) { return super.onTouch(v, event); } @Override public void startVideo() { super.startVideo(); } @Override public void onStateNormal() { super.onStateNormal(); } @Override public void onStatePreparing() { super.onStatePreparing(); } @Override public void onStatePlaying() { super.onStatePlaying(); if(MusicService.isPlaying){ EventBus.getDefault().postSticky(new MusicData(false,0)); Intent intent = new Intent(mContext, MusicService.class); intent.setAction(MusicService.ACTION_Pause); mContext.startService(intent); }else{ Toast.makeText(mContext, "沒有在播放", Toast.LENGTH_SHORT).show(); } } @Override public void onStatePause() { super.onStatePause(); } @Override public void onStateError() { super.onStateError(); } @Override public void onStateAutoComplete() { super.onStateAutoComplete(); } @Override public void onInfo(int what, int extra) { super.onInfo(what, extra); } @Override public void onError(int what, int extra) { super.onError(what, extra); } @Override public void startWindowFullscreen() { super.startWindowFullscreen(); } @Override public void startWindowTiny() { super.startWindowTiny(); } }
//xml
<com.as.apprehendschool.customviews.MyJZVideoPlayerStandard
android:id="@+id/videoplayer"
android:layout_width="match_parent"
android:layout_height="@dimen/x390" />
//使用
/** * 視訊分類 公共介面卡 item video common */ public class SpflRecyclerAdapter extends BaseQuickAdapter<SPfenleiBean.DataBean, BaseViewHolder> { public SpflRecyclerAdapter(int layoutResId, @Nullable List<SPfenleiBean.DataBean> data) { super(layoutResId, data); } @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void convert(BaseViewHolder helper, SPfenleiBean.DataBean item) { helper.setText(R.id.tv_Spfl, item.getDescription()); // 看這裡 final MyJZVideoPlayerStandard videoplayer_Spfl = helper.getView(R.id.videoplayer_Spfl); SPfenleiBean.DataBean.VideoBean videoBean = item.getVideo().get(0); videoplayer_Spfl.setUp( videoBean.getFileurl(), JZVideoPlayer.SCREEN_WINDOW_LIST, item.getTitle()); ImageView thumbImageView = videoplayer_Spfl.thumbImageView; GlideLoadImageUtil.load(mContext,item.getThumb(),thumbImageView); JZVideoPlayer.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } }
//如果在別的地方需要暫停視訊
boolean playing = JZMediaManager.isPlaying();
if(playing){
JZVideoPlayer currentJzvd = JZVideoPlayerManager.getCurrentJzvd();
currentJzvd.onEvent(JZUserAction.ON_CLICK_PAUSE);
JZMediaManager.pause();
currentJzvd.onStatePause();
}
//播放時 如果視訊開著 先暫停 和上面一樣的功能 2選一 JZVideoPlayer currentJzvd = JZVideoPlayerManager.getCurrentJzvd(); if(currentJzvd!=null){ boolean playing = JZMediaManager.isPlaying(); if(playing){ currentJzvd.onEvent(JZUserAction.ON_CLICK_PAUSE); JZMediaManager.pause(); currentJzvd.onStatePause(); } }
嗯,就這些了。
尺寸樣式:
JZVideoPlayerStandard.setVideoImageDisplayType(JZVideoPlayerStandard.FOCUSABLE_AUTO);
還有好多種樣式 比如鋪滿.具體可以點出來 關鍵字母 在 full fill auto.
如果樣式不沒有滿足你的要求可以自己修改,遺憾的是 只會加不會減:
<?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:background="@drawable/butnostroke11"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:descendantFocusability="blocksDescendants">
<!--android:background="@android:color/transparent"-->
<FrameLayout
android:id="@+id/surface_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--android:background="@drawable/butnostroke11"-->
</FrameLayout>
<!--<ImageView-->
<!--android:id="@+id/thumb"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_alignParentBottom="true"-->
<!--android:layout_alignParentLeft="true"-->
<!--android:layout_alignParentStart="true"-->
<!--android:scaleType="fitXY" />-->
<cn.gavinliu.android.lib.shapedimageview.ShapedImageView
android:id="@+id/thumb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:scaleType="fitXY"
app:round_radius="@dimen/x5"
app:shape_mode="round_rect"
app:stroke_color="@color/view_e2"
app:stroke_width="@dimen/x1" />
<LinearLayout
android:id="@+id/layout_bottom"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@drawable/jz_bottom_bg"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="invisible">
<TextView
android:id="@+id/current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:text="00:00"
android:textColor="#ffffff" />
<SeekBar
android:id="@+id/bottom_seek_progress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
android:background="@null"
android:max="100"
android:maxHeight="1dp"
android:minHeight="1dp"
android:paddingBottom="8dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="8dp"
android:progressDrawable="@drawable/jz_bottom_seek_progress"
android:thumb="@drawable/jz_bottom_seek_thumb" />
<TextView
android:id="@+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:textColor="#ffffff" />
<TextView
android:id="@+id/clarity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:paddingLeft="20dp"
android:text="clarity"
android:textAlignment="center"
android:textColor="#ffffff" />
<ImageView
android:id="@+id/fullscreen"
android:layout_width="52.5dp"
android:layout_height="fill_parent"
android:paddingLeft="14dp"
android:paddingRight="14dp"
android:scaleType="centerInside"
android:src="@drawable/jz_enlarge" />
</LinearLayout>
<ProgressBar
android:id="@+id/bottom_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="1.5dp"
android:layout_alignParentBottom="true"
android:max="100"
android:progressDrawable="@drawable/jz_bottom_progress" />
<ImageView
android:id="@+id/back_tiny"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginLeft="6dp"
android:layout_marginTop="6dp"
android:background="@drawable/jz_click_back_tiny_selector"
android:visibility="gone" />
<RelativeLayout
android:id="@+id/layout_top"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@android:color/transparent"
android:gravity="center_vertical">
<ImageView
android:id="@+id/back"
android:layout_width="23dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:paddingLeft="12dp"
android:paddingStart="12dp"
android:scaleType="centerInside"
android:src="@drawable/jz_click_back_selector" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="12dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginStart="12dp"
android:layout_toEndOf="@+id/back"
android:layout_toLeftOf="@+id/battery_time_layout"
android:layout_toRightOf="@+id/back"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#ffffff"
android:textSize="18sp" />
<LinearLayout
android:id="@+id/battery_time_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="14dp"
android:layout_marginRight="14dp"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="@+id/battery_level"
android:layout_width="23dp"
android:layout_height="10dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/jz_battery_level_10" />
<TextView
android:id="@+id/video_current_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:maxLines="1"
android:textColor="#ffffffff"
android:textSize="12.0sp" />
</LinearLayout>
</RelativeLayout>
<ProgressBar
android:id="@+id/loading"
android:layout_width="@dimen/jz_start_button_w_h_normal"
android:layout_height="@dimen/jz_start_button_w_h_normal"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminateDrawable="@drawable/jz_loading"
android:visibility="invisible" />
<LinearLayout
android:id="@+id/start_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical">
<ImageView
android:id="@+id/start"
android:layout_width="@dimen/jz_start_button_w_h_normal"
android:layout_height="@dimen/jz_start_button_w_h_normal"
android:src="@drawable/jz_click_play_selector" />
</LinearLayout>
<TextView
android:id="@+id/replay_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/start_layout"
android:layout_centerHorizontal="true"
android:layout_marginTop="6dp"
android:text="@string/replay"
android:textColor="#ffffff"
android:textSize="12sp"
android:visibility="invisible" />
<LinearLayout
android:id="@+id/retry_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/video_loading_faild"
android:textColor="@android:color/white"
android:textSize="14sp" />
<TextView
android:id="@+id/retry_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@drawable/retry_bg"
android:paddingBottom="4dp"
android:paddingLeft="9dp"
android:paddingRight="9dp"
android:paddingTop="4dp"
android:text="@string/click_to_restart"
android:textColor="@android:color/white"
android:textSize="14sp" />
</LinearLayout>
</RelativeLayout>
注意id 不可以變.
Android 視訊播放器
Android
1.呼叫系統播放器,
2.VideoView 結合 MediaController
3.SurfaceView +MediaPlayer
4.第三方視訊播放器:UniversalVideoview JiaoZiVideoPlayer (7K+star)...
主要介紹一下JiaoZiVideoPlayer
Manifest:
android:configChanges="orientation|screenSize|keyboardHidden"
1.高度自定義Ui
通過自定義 extends JZVideoPlayerStandard
@Override
public int getLayoutId() {
// return cn.jzvd.R.layout.jz_layout_standard;//預設返回
return R.layout.custom_jzvideo_palyer;//自定義
}
需要注意的是XML資源id不能變,可以修改顏色,新增控制元件,無ID控制元件刪除.
2.各個階段的狀態
JZVideoPlayerStandard暴露出很多狀態方便及時回撥,比如:專案裡有音樂和視訊,要求不能同時播放,在onStatePlaying(視訊即將進入播放),可以在這個方法裡進行暫停音樂的操作,如果視訊需要付費/解鎖才能播放,可以在startVideo()這個 方法裡進行判斷,
3.多尺寸展示
Android 手機螢幕視訊一般以16:9為主流.視訊的尺寸可以自定義全屏鋪滿,或者視訊原尺寸.
JZVideoPlayerStandard.setVideoImageDisplayType(JZVideoPlayerStandard.FOCUSABLE_AUTO);//原尺寸
這裡有很多,關鍵字Focusable,auto,screen.
可以通過新增ViewOutlineProvider 為播放介面新增圓角.
4.列表滑動小窗//沒用到
5.自帶重力感應
在你手動設定了會禁掉.
// JZVideoPlayerStandard.FULLSCREEN_ORIENTATION= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
// JZVideoPlayerStandard.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
sensorEventListener = new JZVideoPlayerStandard.JZAutoFullscreenListener();
onResume
Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(sensorEventListener);
JZVideoPlayer.releaseAllVideos();
}
6.視訊快取 AndroidVideoCache
接入簡單.Demo 有例子
7.還提供了JZVideoPlayerManager JZMediaManager 管理類.
JZVideoPlayerManager.getCurrentJzvd();獲取當前正在播放的播放器
JZMediaManager.isPlaying();當前是否在播放
//強行暫停!
currentJzvd.onEvent(JZUserAction.ON_CLICK_PAUSE);
JZMediaManager.pause();
currentJzvd.onStatePause();
//小白使用文件
關於有些地方回撥沒用
可以在fragment不可見;瞭然後調
@Override public void onSupportInvisible() { super.onSupportInvisible(); JZVideoPlayer.releaseAllVideos(); }