1.布局以及实例化
<VideoView
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/video_view_sys"/>
@BindView(R.id.video_view_sys)
VideoViewvideoViewSys;
2.播放视频的地址
videoViewSys.setVideoURI(uri);
videoViewSys.setVideoPath(mediaItem.getData());
3.准备监听
videoViewSys.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
//1.得到视频的总时长
int duration =videoViewSys.getDuration();
//2.发消息更新播放进度
mHandler.sendEmptyMessage(VIDEO_PROGRESS);
videoViewSys.start();
}
});
4.播放错误监听
videoViewSys.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
Toast.makeText(SysVideoPlayActivity.this, "播放出错了", Toast.LENGTH_LONG).show();
return true;
}
});
5.播放完成监听
videoViewSys.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Toast.makeText(SysVideoPlayActivity.this, "播放完成", Toast.LENGTH_LONG).show();
}
});
6.系统自带的控制条工具
videoViewSys.setMediaController(new MediaController(this));
7.线程更新,播放进度条
@SuppressLint("HandlerLeak")
private HandlermHandler =new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case VIDEO_PROGRESS:
int currentPosition =videoViewSys.getCurrentPosition();//得到当前的播放进度
seekbarVideo.setProgress(currentPosition);//设置进度条位置
removeMessages(VIDEO_PROGRESS);//每秒更新一次
sendEmptyMessageDelayed(VIDEO_PROGRESS, 1000);
break;
}
}
};
8.暂停
videoViewSys.pause();
9.播放
videoViewSys.start();
10.设置手机不锁屏
videoViewSys.setKeepScreenOn(true); //videoview的方法
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //這個是 activity 的方法
网友评论