VideoView视频播放器
1.获取本地视频
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ContentResolver contentResolver;
private VideoAdapter adapter;
//创建集合
private Listlist;
//动态读取SD卡权限
private static final int REQUEST_EXTERNAL_STORAGE =1;
private static String[]PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
//动态获取权限
verifyStoragePermissions(this);
contentResolver =this.getContentResolver();//获取内容解析者
getVideo();
adapter.setOnItemClickLinstener(new VideoAdapter.OnItemClickLinstener() {
@Override
public void onItemClick(int postion) {
//播放视频
Toast.makeText(MainActivity.this,"点击了" + postion, Toast.LENGTH_LONG).show();
Intent intent =new Intent(MainActivity.this, VideoActivity.class);
intent.putExtra("URI",list.get(postion).getData());
startActivity(intent);
}
});
}
private void init() {
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager manager =new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
}
/**
* 获取视频
*/
private void getVideo() {
list =new ArrayList<>();
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] objs = {
MediaStore.Video.Media.DISPLAY_NAME,//视频在sdcard中的名字
MediaStore.Video.Media.DURATION,//时长
MediaStore.Video.Media.SIZE,//大小
MediaStore.Video.Media.DATA,//绝对地址
MediaStore.Video.Media.ARTIST//出品人
};
Cursor cursor =contentResolver.query(uri, objs,null,null,null);
if (cursor !=null) {//判断是否为空
while (cursor.moveToNext()) {
MediaBean bean =new MediaBean();
String name = cursor.getString(0);
long duration = cursor.getLong(1);
long size = cursor.getLong(2);
String data = cursor.getString(3);
String artist = cursor.getString(4);
bean.setName(name);
bean.setDuration(duration);
bean.setSize(size);
bean.setData(data);
bean.setArtist(artist);
//设置完数据添加到集合
list.add(bean);
}
//将数据设置到适配器中去
adapter =new VideoAdapter(this,list);
recyclerView.setAdapter(adapter);
//关闭游标
cursor.close();
}
}
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(activity,PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
}
}
}
2.适配器
public class VideoAdapter extends RecyclerView.Adapter {
private Context context;
private Listlist;
public VideoAdapter(Context context, List list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.itrm_video, parent,false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder,final int position) {
holder.name.setText(list.get(position).getName());
holder.time.setText(list.get(position).getDuration() +"");
holder.zuozhe.setText(list.get(position).getArtist());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickLinstener.onItemClick(position);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MyHolderextends RecyclerView.ViewHolder {
TextViewname;
TextViewtime;
TextViewzuozhe;
public MyHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
time = itemView.findViewById(R.id.time);
zuozhe = itemView.findViewById(R.id.zuozhe);
}
}
//条目点击事件的监听
public OnItemClickLinsteneronItemClickLinstener;
public interface OnItemClickLinstener {
void onItemClick(int postion);
}
public void setOnItemClickLinstener(OnItemClickLinstener linstener) {
this.onItemClickLinstener = linstener;
}
}
3.视频bean类
public class MediaBean {
private Stringname;//名字
private long duration;//时长
private long size;//大小
private Stringdata;//绝对地址
private Stringartist;//出品人
public String getName() {
return name;
}
public long getDuration() {
return duration;
}
public long getSize() {
return size;
}
public String getData() {
return data;
}
public String getArtist() {
return artist;
}
public void setName(String name) {
this.name = name;
}
public void setDuration(long duration) {
this.duration = duration;
}
public void setSize(long size) {
this.size = size;
}
public void setData(String data) {
this.data = data;
}
public void setArtist(String artist) {
this.artist = artist;
}
}
4.播放器页面
public class VideoActivity extends Activity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {
private VideoViewvideoView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
//获得视频uri
Intent intent = getIntent();
String uri = intent.getStringExtra("URI");
videoView = findViewById(R.id.video);
//如果播放网络视频 ,获取到网络连接
//String url = "https:xxxxxxxx";
//Uri parse = Uri.parse( url );
videoView.setVideoURI(parse);
//设置准备好视频的监听
videoView.setOnPreparedListener(this);
//设置出错监听
videoView.setOnErrorListener(this);
//设置播放完成监听
videoView.setOnCompletionListener(this);
//设置控制器
videoView.setMediaController(new MediaController(this));
}
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
@Override
public boolean onError(MediaPlayer mp,int what,int extra) {
Toast.makeText(this,"出错了", Toast.LENGTH_LONG).show();
return false;
}
@Override
public void onCompletion(MediaPlayer mp) {
finish(); //播放完成后 关闭播放页面
// mp.start(); //重复播放
}
}
5.清单文件添加读取sd卡权限
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_EXTERNAL_STORAGE
6.0以上版本 记得动态获取权限
6.xml文件
视频页面
Main页面就是一个 recyclerView