手機影音6--視頻播放器的準系統(3),6--準系統
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