项目地址:[https://github.com/YanInfo/VideoPlayer](() 欢迎Star
[](()二,关于JiaoZiVideoPlayer
功能很全面的一个三方视频播放器
项目地址:[JiaoZiVideoPlayer官方项目](()
项目说明:[JiaoZiVideoPlayer的说明](()
[](()三,实现的核心代码
1, 添加类库
implementation ‘fm.jiecao:jiecaovideoplayer:5.5.4’
implementation ‘com.zhy:base-adapter:3.0.3’
这里第一个是添加三方播放器,第二个是添加这里要用到的万能适配器
添加网络权限和本地读写权限
2,布局文件
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><ListView xmlns:android=“http://schemas.android.com/apk/res/android”
android:id="@+id/listview"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical” />
这里定义了一个ListView,通过下面的适配器来适配item_video.xml布局
item_video.cml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“vertical”>
<fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
android:id="@+id/player_list_video"
android:layout_width=“match_parent”
android:layout_height=“190dp” />
<RelativeLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:paddingBottom=“8dp”
android:paddingTop=“8dp”>
<ImageView
android:id="@+id/img_video_icon"
android:layout_width=“40dp”
android:layout_height=“40dp”
android:layout_marginLeft=“12dp”
android:src="@drawable/o6" />
<TextView
android:id="@+id/tv_video_userName"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignBottom="@+id/tv_video_comment"
android:layout_marginLeft=“12dp”
android:layout_marginBottom=“11dp”
android:layout_toRightOf="@id/img_video_icon"
android:text=“不舔瓶盖” />
<TextView
android:id="@+id/tv_video_comment"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentTop=“true”
android:layout_alignParentEnd=“true”
android:layout_alignParentRight=“true”
android:layout_marginTop=“10dp”
android:layout_marginEnd=“15dp”
android:layout_marginRight=“14dp”
android:drawableLeft="@drawable/image"
android:text=“36”
android:textSize=“12sp” />
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_centerVertical=“true”
android:layout_marginRight=“27dp”
android:layout_toLeftOf="@id/tv_video_comment"
android:text=“3万次播放” />
这里添加了一个Video和一个相对布局,中间的图标和一些xml文件请参照GitHub地址
3,适配器代码
public class VideoAdapter extends CommonAdapter {
private Context mContext;
/**
-
构造函数
-
@param context
-
@param datas
-
@param layoutId
*/
public VideoAdapter(Context context, List datas, int layoutId) {
super(context, layoutId, datas);
this.mContext = context;
}
@Override
protected void convert(ViewHolder viewHolder, String url, int position) {
JCVideoPlayerStandard player = viewHolder.getView(R.id.player_list_video);
if (player != null) {
player.release();
}
player.setUp(url, JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, “爸爸去买几个橘子就回”);
}
}
适配器很简单
4,主活动代码
/**
-
主活动
-
@author zhangyan
*/
public class MainActivity extends AppCompatActivity {
//在线视频
private String videoUrl = “http://video.jiecao.fm/5/1/%E8%87%AA%E5%8F%96%E5%85%B6%E8%BE%B1.mp4”;
private String videoUrl1 = “http://112.253.22.157/17/z/z/y/u/zzyuasjwufnqerzvyxgkuigrkcatxr/hc.yinyuetai.com/D046015255134077DDB3ACA0D7E68D45.flv”;
private String videoUrl2 = “https://key002.ku6.com/xy/d7b3278e106341908664638ac5e92802.mp4”;
private String videoUrl3 = “https://key002.ku6.com/xy/d7b3278e106341908664638ac5e92802.mp4”;
private String videoUrl4 = “http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4”;
private String videoUrl5 = “http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4”;
//本地视频
String localUrl = Environment.getExternalStorageDirectory().getAbsolutePath() + “/video/aaaa.mp4”;
private ListView listView;
private ArrayList datas;
private JCVideoPlayerStandard currPlayer;
private VideoAdapter adapter;
private ImageView image;
/**
- 滑动监听
*/
private AbsListView.OnScrollListener onScrollListener;
/**
- 当前第一个可见的item
*/
private int firstVisible;
/**
- 当前可见的item个数
*/
private int visibleCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
initDatas();
initListener();
}
/**
- 初始化
*/
private void initDatas() {
datas = new ArrayList<>();
datas.add(videoUrl);
// mediaPlayer(videoUrl);
datas.add(videoUrl1);
datas.add(videoUrl2);
datas.add(videoUrl3);
datas.add(videoUrl4);
datas.add(videoUrl5);
datas.add(localUrl);
adapter = new VideoAdapter(MainActivity.this, datas, R.layout.item_video);
listView.setAdapter(adapter);
}
/**
- 加载视频第一帧
*/
private void mediaPlayer(String url) {
MediaMetadataRetriever media = new MediaMetadataRetriever();
//实例化File对象,文件路径为/storage/sdcard/Movies/music1.mp4
if(!url.isEmpty()){
media.setDataSource(url);
Bitmap bitmap = media.getFrameAtTime();
image = (ImageView)this.findViewById(R.id.player_list_video);
if(bitmap!=null){
image.setImageBitmap(bitmap);//设置ImageView显示的图片
}else{