activity_main
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.animee.app2.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_local"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
item_local_video
<?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="wrap_content"
android:padding="5dp">
<SurfaceView
android:id="@+id/item_surface"
android:layout_width="match_parent"
android:layout_height="220dp" />
<ImageView
android:id="@+id/item_iv"
android:layout_width="match_parent"
android:layout_height="220dp"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/item_tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textColor="@color/colorPrimary"
android:text="内容部分"/>
<TextView
android:id="@+id/item_tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/item_surface"
android:textSize="18sp"
android:text="播放时长:60min"/>
<TextView
android:id="@+id/item_tv_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/item_tv_time"
android:textSize="18sp"
android:text="视频大小:1000M"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorAccent"
android:layout_below="@+id/item_tv_size"
android:layout_marginTop="10dp"/>
</RelativeLayout>
MainActivity
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView rv;
private List<LocalVideoInfo>mDatas;
LocalVideoAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = (RecyclerView) findViewById(R.id.rv_local);
// 设置布局管理器
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(manager);
mDatas = new ArrayList<>();
// 设置适配器对象
adapter = new LocalVideoAdapter(this, mDatas);
rv.setAdapter(adapter);
// 加载sd卡当中的视频,即真实的数据源
loadData();
}
private void loadData() {
List<LocalVideoInfo> list = new ArrayList<>();
// 1.获取ContentResolver对象
ContentResolver resolver = getContentResolver();
// 2.获取Uri地址
Uri videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
// 3.开始查询系统视频数据库
Cursor cursor = resolver.query(videoUri, null, null, null, MediaStore.Video.Media.DISPLAY_NAME);
// 4.移动cursor指针
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DURATION));
long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.SIZE));
LocalVideoInfo info = new LocalVideoInfo(name,path,duration,size);
list.add(info);
}
mDatas.addAll(list);
adapter.notifyDataSetChanged();
}
@Override
protected void onDestroy() {
super.onDestroy();
adapter.destoryPlayer();
}
}
LocalVideoInfo
public class LocalVideoInfo {
private String name;
private String path;
private long duration;
private long size;
@Override
public String toString() {
return "LocalVideoInfo{" +
"name='" + name + '\'' +
", path='" + path + '\'' +
", duration=" + duration +
", size=" + size +
'}';
}
public LocalVideoInfo() {
}
public LocalVideoInfo(String name, String path, long duration, long size) {
this.name = name;
this.path = path;
this.duration = duration;
this.size = size;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
}
LocalVideoAdapter
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* Created by Administrator on 2017/12/20.
*/
public class LocalVideoAdapter extends RecyclerView.Adapter<LocalVideoAdapter.LocalViewHolder>{
private Context context;
private List<LocalVideoInfo>mDatas;
private MediaPlayer player;
// 记录哪个位置播放视频了
private int currentPosition = -1;
public LocalVideoAdapter(Context context, List<LocalVideoInfo> mDatas) {
this.context = context;
this.mDatas = mDatas;
player = new MediaPlayer();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
player.release();
}
});
}
public void destoryPlayer(){
if (player!=null) {
player.stop();
player.release();
}
}
@Override
public LocalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.item_local_video,parent,false);
LocalViewHolder holder = new LocalViewHolder(itemView);
return holder;
}
@Override
public void onBindViewHolder(LocalViewHolder holder, int position) {
final LocalVideoInfo info = mDatas.get(position);
holder.mTitleTv.setText(info.getName());
// 获取播放时间
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
String time = sdf.format(new Date(info.getDuration()));
holder.mDurationTv.setText("播放时长:"+time);
// 获取视频的大小
long mSize = info.getSize() / 1024 / 1024;
holder.mSizeTv.setText("视频大小:"+mSize+"M");
// 获取视频缩略图,显示缩略图
Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(info.getPath(), MediaStore.Video.Thumbnails.MINI_KIND);
holder.mThumbIv.setImageBitmap(thumbnail);
holder.mThumbIv.setTag(position);
// 判断当前位置是否是需要播放视频的位置。
holder.mThumbIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer pos = (Integer) v.getTag();
currentPosition = pos;
notifyDataSetChanged();
}
});
if (currentPosition == position) {
if (player.isPlaying()) {
player.stop();
}
// 说明当前正在被创建的view,应该播放视频
// 隐藏ImageView,展示SurfaceView
holder.mSurfaceView.setVisibility(View.VISIBLE);
holder.mThumbIv.setVisibility(View.INVISIBLE);
// 获取SurfaceHolder对象
SurfaceHolder surfaceHolder = holder.mSurfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
player.reset(); //重置媒体播放器
player.setDisplay(holder); //设置播放器播放的位置
try {
player.setDataSource(info.getPath());
player.prepare(); //本地视频使用同步,网络视频使用异步
player.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}else{
// 不需要播放视频
holder.mSurfaceView.setVisibility(View.INVISIBLE);
holder.mThumbIv.setVisibility(View.VISIBLE);
}
}
@Override
public int getItemCount() {
return mDatas.size();
}
class LocalViewHolder extends RecyclerView.ViewHolder{
SurfaceView mSurfaceView;
ImageView mThumbIv;
TextView mTitleTv,mDurationTv,mSizeTv;
public LocalViewHolder(View itemView) {
super(itemView);
mSurfaceView = (SurfaceView) itemView.findViewById(R.id.item_surface);
mThumbIv = (ImageView) itemView.findViewById(R.id.item_iv);
mTitleTv = (TextView)itemView.findViewById(R.id.item_tv_title);
mDurationTv = (TextView)itemView.findViewById(R.id.item_tv_time);
mSizeTv = (TextView)itemView.findViewById(R.id.item_tv_size);
}
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>