参看:Android自定义视频播放器(一):https://blog.csdn.net/zxd1435513775/article/details/81507909 参看:Android自定义视频播放器(二):https://blog.csdn.net/zxd1435513775/article/details/81533053 参看:Android自定义视频播放器(三):https://blog.csdn.net/zxd1435513775/article/details/81535065 参看:Android自定义视频播放器(四):https://blog.csdn.net/zxd1435513775/article/details/81562032 参看:Android自定义视频播放器(五):https://blog.csdn.net/zxd1435513775/article/details/81626882
一、引言现在已经差不多完成了一个自定义的视频播放器,还有一个让视频全屏播放的按钮事件没有绑定,下面会介绍。此外还要一个非常重要的功能就是,已经写好了自定义的播放器,那么就应该能被其他应用调起,来播放视频,下面来完善这两个功能。
二、全屏按钮事件的绑定 1、调用设置全屏方法private void setFullScreenAndDefault() {
if (isFullScreen) {
//默认
setVideoType(DEFAULT_SCREEN);
} else {
//全屏
setVideoType(FULL_SCREEN);
}
}
2、setVideoType()方法
private void setVideoType(int defaultScreen) {
switch (defaultScreen) {
case FULL_SCREEN://全屏
//1.设置视频画面的大小,屏幕有多大就是多大
videoview.setVideoSize(screenWidth, screenHeight);
//2.设置按钮的状态-默认
btnVideoSiwchScreen.setBackgroundResource(R.drawable.btn_video_siwch_screen_default_selector);
isFullScreen = true;
case DEFAULT_SCREEN://默认
//1.设置视频画面的大小
//视频真实的宽和高
int mVideoWidth = videoWidth;
int mVideoHeight = videoHeight;
//屏幕的宽和高
int width = screenWidth;
int height = screenHeight;
if (mVideoWidth * height < width * mVideoHeight) {
width = height * mVideoWidth / mVideoHeight;
} else if (mVideoWidth * height > width * mVideoHeight) {
height = width * mVideoHeight / mVideoWidth;
}
videoview.setVideoSize(width, height);
//2.设置按钮的状态--全屏
btnVideoSiwchScreen.setBackgroundResource(R.drawable.btn_video_siwch_screen_full_selector);
isFullScreen = false;
break;
}
}
3、当视频Prepared后获取视频的长和宽
videoWidth = mp.getVideoWidth();
videoHeight = mp.getVideoHeight();
4、获取手机屏幕的长和宽
//得到屏幕的宽和高
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
screenWidth = displayMetrics.widthPixels;
screenHeight = displayMetrics.heightPixels;
5、说明
此处除了按钮的点击事件,还可以实现双击屏幕,就可以实现屏幕大小的切换,方法的调用,放在自定义的MySimpleOnGestureListener 中onDoubleTapEvent()中方法即可。
三、让第三方应用调起 1、使用隐式意图
2、调起方,调用方法
//1.调起系统所有的播放-隐式意图
Intent intent = new Intent();
intent.setDataAndType(Uri.parse("视频播放地址"),"video/*");
context.startActivity(intent);
3、视频播放器就会被调起并且播放
uri = getIntent().getData();
此处在前文中,介绍过一点,就是传递过来的结果如果是集合的情况和其他第三方应用的情况。留单独获取一个uri的情况就是在这里出现。
4、设置播放地址 videoview.setVideoURI(uri);
四、效果图
1、其他应用调用效果图