首页 > 代码库 > 手机影音6--视频播放器的基本功能(3)
手机影音6--视频播放器的基本功能(3)
1.自定义VideoView
1_自定义VideoView-增加设置视频大小方法
public class VideoView extends android.widget.VideoView { /** * Android系统在更加xml布局找这个类,并且实例化的时候,用该构造方法实例化 * * @param context * @param attrs */ public VideoView(Context context, AttributeSet attrs) { super(context, attrs); } public VideoView(Context context) { super(context); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); } /** * 设置视频画面的宽和高 * @param videoWidth * @param videoHeight */ public void setVideoSize(int videoWidth, int videoHeight) { ViewGroup.LayoutParams layoutParams =getLayoutParams(); layoutParams.width = videoWidth; layoutParams.height = videoHeight; setLayoutParams(layoutParams); } }
2_得到屏幕高和宽方法
在播放器中
wm = (WindowManager) getSystemService(WINDOW_SERVICE); screenWidth = wm.getDefaultDisplay().getWidth(); screenHeight = wm.getDefaultDisplay().getHeight(); DisplayMetrics displayMetrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); screenWidth = displayMetrics.widthPixels; screenHeight = displayMetrics.heightPixels;
3_视频默认和全屏
一播放起来,在准备好了中设置视频播放默认
/** * true全屏 * flase默认 */ private boolean isFullScreen = false; /** * 视频全屏和默认 * @param type */ public void setVideoType(int type){ switch (type) { case SCREEN_FULL: videoview.setVideoSize(screenWidth, screenHeight); isFullScreen = true; btn_switch_screen.setBackgroundResource(R.drawable.btn_screen_dafult_selector); break; case SCREEN_DEFULT: //视频的宽 int mVideoWidth = videoWidth; //视频的高 int mVideoHeight = videoHeight; //屏幕的宽 int width = screenWidth; //屏幕的宽 int height = screenHeight; if (mVideoWidth > 0 && mVideoHeight > 0) { if ( mVideoWidth * height > width * mVideoHeight ) { //Log.i("@@@", "image too tall, correcting"); height = width * mVideoHeight / mVideoWidth; } else if ( mVideoWidth * height < width * mVideoHeight ) { //Log.i("@@@", "image too wide, correcting"); width = height * mVideoWidth / mVideoHeight; } else { //Log.i("@@@", "aspect ratio is correct: " + //width+"/"+height+"="+ //mVideoWidth+"/"+mVideoHeight); } } videoview.setVideoSize(width, height); btn_switch_screen.setBackgroundResource(R.drawable.btn_screen_full_selector); isFullScreen = false; break; } }
4_屏幕保持不锁屏
//设置屏幕不锁屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
5_点击按钮的时候实现切换播放模式
case R.id.btn_switch_screen: if(isFullScreen){ setVideoType(SCREEN_DEFUALT); }else{ setVideoType(SCREEN_FULL); } break
手机影音6--视频播放器的基本功能(3)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。