码迷,mamicode.com
首页 > 移动开发 > 详细

手机影音6--视频播放器的基本功能(3)

时间:2016-11-14 07:43:10      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:tpi   技术   default   private   log   efault   cte   boolean   xtend   

技术分享

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)

标签:tpi   技术   default   private   log   efault   cte   boolean   xtend   

原文地址:http://www.cnblogs.com/ganchuanpu/p/6060531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!