Mobile phone audio and video 6-basic functions of video players (3) and 6-basic functions

Source: Internet
Author: User

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

  

  

  

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.