开源地址:https://github.com/open-android/JieCaoVideoPlayer
简书:http://www.jianshu.com/p/618a5820261c
##主要特点
- 视频全屏播放和浮层小窗播放
- 可以完全自定义UI
- 能在
ListView
、ViewPager
和ListView
、ViewPager
和Fragment
等多重嵌套模式下全屏工作 - 手势修改进度和音量
- 视频大小的屏幕适配,宽或长至少有两个对边是充满屏幕的,另外两个方向居中
- 可以在加载、暂停、播放等各种状态中正常进入全屏和退出全屏
- 重力感应自动全屏
- WebView嵌套本地视频控件
- [支持https和rtsp]{https://developer.android.com/guide/topics/media/media-formats.html}
- 小于 100kb
##效果
- 爱生活,爱学习,更爱做代码的搬运工,分类查找更方便请下载黑马助手app
使用步骤
1. 在project的build.gradle添加如下代码(如下图)
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
2. 在Module的build.gradle添加依赖
compile 'com.github.open-android:JieCaoVideoPlayer:0.1.0'
3.添加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
4.在Activity中添加如下代码
listView = (ListView) findViewById(R.id.listview);
adapterVideoList = new VideoListAdapter(this);
listView.setAdapter(adapterVideoList);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorEventListener = new JCVideoPlayer.JCAutoFullscreenListener();
5.在Activity监听如下生命周期方法
@Override
public void onBackPressed() {
if (JCVideoPlayer.backPress()) {
return;
}
super.onBackPressed();
}
@Override
protected void onResume() {
super.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);
JCVideoPlayer.releaseAllVideos();
}
6.新建一个Adapter继承BaseAdapter并且复制如下代码
public static final String TAG = "JieCaoVideoPlayer";
int[] videoIndexs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Context context;
int pager = -1;
public VideoListAdapter(Context context) {
this.context = context;
}
public VideoListAdapter(Context context, int pager) {
this.context = context;
this.pager = pager;
}
@Override
public int getCount() {
return pager == -1 ? videoIndexs.length : 4;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (null == convertView) {
viewHolder = new ViewHolder();
LayoutInflater mInflater = LayoutInflater.from(context);
convertView = mInflater.inflate(R.layout.item_videoview, null);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.jcVideoPlayer = (JCVideoPlayerStandard) convertView.findViewById(R.id.videoplayer);
if (pager == -1) {
viewHolder.jcVideoPlayer.setUp(
VideoConstant.videoUrls[0][position], JCVideoPlayer.SCREEN_LAYOUT_LIST,
VideoConstant.videoTitles[0][position]);
System.out.println("fdsfdsfdsfdsfa setup " + position);
Picasso.with(convertView.getContext())
.load(VideoConstant.videoThumbs[0][position])
.into(viewHolder.jcVideoPlayer.thumbImageView);
} else {
viewHolder.jcVideoPlayer.setUp(
VideoConstant.videoUrls[pager][position], JCVideoPlayer.SCREEN_LAYOUT_LIST,
VideoConstant.videoTitles[pager][position]);
Picasso.with(convertView.getContext())
.load(VideoConstant.videoThumbs[pager][position])
.into(viewHolder.jcVideoPlayer.thumbImageView);
}
return convertView;
}
class ViewHolder {
JCVideoPlayerStandard jcVideoPlayer;
}
7.添加Adapter的item布局R.layout.item_videoview
<fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
android:id="@+id/videoplayer"
android:layout_width="match_parent"
android:layout_height="200dp" />
8.在AndroidManifest.xml中
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="portrait" /> <!-- or android:screenOrientation="landscape"-->
完成如上步骤就可以实现今日头条视频播放列表功能 , 更多功能请参考样例工程app
欢迎关注微信公众号