1.自定義VideoView 1_自定義VideoView-增加設置視頻大小方法 2_得到屏幕高和寬方法 在播放器中 3_視頻預設和全屏 一播放起來,在準備好了中設置視頻播放預設 4_屏幕保持不鎖屏 5_點擊按鈕的時候實現切換播放模式 ...
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