Mobile phone audio and video 6-basic functions of video players (3) and 6-basic functions
1. Custom VideoView
1 _ custom VideoView-added video size setting method
Public class VideoView extends android. widget. videoView {/*** when the Android system finds this class in a more xml layout and instantiates it, instantiate ** @ 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 );} /*** set the width and height of the video image * @ param videoWidth * @ param videoHeight */public void setVideoSize (int videoWidth, int videoHeight) {ViewGroup. layoutParams layoutParams = getLayoutParams (); layoutParams. width = videoWidth; layoutParams. height = videoHeight; setLayoutParams (layoutParams );}}
2_obtain the screen height and width
In the player
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_video default and full screen
When playing the video, set the default value for video playing in preparation.
/*** True full screen * flase default */private boolean isFullScreen = false;/*** full screen video and default * @ 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: // video width int mVideoWidth = videoWidth; // The video height int mVideoHeight = videoHeight; // The Screen width int width = screenWidth; // The Screen width 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_screen persistence without screen lock
// Set the screen to not lock getWindow (). setFlags (WindowManager. LayoutParams. FLAG_KEEP_SCREEN_ON, WindowManager. LayoutParams. FLAG_KEEP_SCREEN_ON );
5 _ switching the playback mode when you click the button
case R.id.btn_switch_screen:if(isFullScreen){ setVideoType(SCREEN_DEFUALT);}else{ setVideoType(SCREEN_FULL);}break