继续找播放器,找到了饺子播放器, JZVideo,据说它可以高度自定义,而且还可以结合ijkplayer的内核之类的。
这好像是之前的项目 点击这里,已经10.2k star了,现在的项目在 这里,最新版本应该是7.4.x了,但是新版本的源码不太好自定义,主要是我看的教程时6.4.2的,所以就换了。
特点
- 可以完全自定义UI和任何功能。
- 很容易切换播放引擎,支持的视频格式和协议取决于播放引擎,android.media.MediaPlayer或者ijkplayer等。
- 可以检测列表的滑动。
- 可以嵌入到多个控件中,比如ListView、Fragment,RecyclerView等等,多重嵌套也可以。
- 可以实现全屏播放和小窗播放。
- 可以在加载、暂停和播放等状态下切换全屏和退出全屏。
- 可以进行屏幕适配,铺满全屏或者全屏裁剪。
- 好像还支持重力感应自动横屏。
- 全屏后可以用手势修改进度和音量。
- Home键退出界面暂停播放,返回界面的时候继续播放。
- 等等etc,应该还可以有很多很多,等待开发。
配置
下面配置一下,引入JZVideo到项目里。
添加依赖
在build.gradle中添加
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestCompile('com.android.support:support-annotations:26.1.0') {
force = true
}
......
//饺子播放器
compile 'cn.jzvd:jiaozivideoplayer:6.2.12'
}
好象这样也行implementation ‘cn.jzvd:jiaozivideoplayer:7.0.3’
添加布局
在layout文件中添加控件,外层最好添加一个Layout。
<LinearLayout
android:layout_width="400dp"
android:layout_height="200dp">
<cn.jzvd.JZVideoPlayerStandard
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></cn.jzvd.JZVideoPlayerStandard>
</LinearLayout>
添加逻辑
修改java代码,设置视频地址和缩略图等,缩略图可以用Glide
传入,还需要重写一下声明周期方法里的东西。
JZVideoPlayerStandard VideoPlayer=findViewById(R.id.video_view);
VideoPlayer.setUp("http://jzvd.nathen.cn/c6e3dc12a1154626b3476d9bf3bd7266/6b56c5f0dc31428083757a45764763b0-5287d2089db37e62345123a1be272f8b.mp4",
JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,
"饺子闭眼睛");
JZVideoPlayer.SAVE_PROGRESS = false;
try {
VideoPlayer.thumbImageView.setImageResource(R.drawable.diamond);
}catch(Exception e){
e.printStackTrace();
}
@Override
public void onBackPressed() {
if (JZVideoPlayer.backPress()) {
return;
}
super.onBackPressed();
}
@Override
protected void onPause() {
super.onPause();
JZVideoPlayer.releaseAllVideos();
}
修改AndroidManifest.xml
添加一下属性
<activity android:name=".VideoActivity"
android:configChanges="orientation|keyboardHidden|screenSize">
这样子最基本的就搞定了,然后开始自定义。
小问题
因为我的软件是横屏的,在视频退出全屏后,会暂停,而且回到竖屏。
展示图
自定义
这里需要用到上面说的到的6.4.2版本源码,没有的话问题不大,我把代码放出来。
自定义UI
编写一个新的layout布局文件,作为我们自定义的控件,叫custon_videoview.xml,将JZVideo-6.4.2\jiaozivideoplayer\src\main\res\layout
中的jz_layout_std.xml
代码全部复制进来。
然后可以按照这个内容和布局,自己添加新的控件。
filename: custon_videoview.xml
<?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="@android:color/black"
android:descendantFocusability="afterDescendants">
<FrameLayout
android:id="@+id/surface_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ImageView
android:id="@+id/thumb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:background="#000000"
android:scaleType="fitCenter" />
<LinearLayout
android:id="@+id/layout_bottom"
android:layout_width="match_parent"
android:layout_height="50dp"