Android Surfaceview Adjust the screen when playing video

Source: Internet
Author: User

For a horizontal screen recorded video on the horizontal screen playback, for vertical screen recorded video on the vertical screen playback.

in the Mainifest file, add the following properties to the activity that is responsible for playback "

    android:configchanges="orientation|keyboardhidden|screensize"

To override the Acitivity onconfigurationchanged method:

    @Override        publicvoid  onconfigurationchanged (Configuration Newconfig) {            super.onconfigurationchanged (newconfig);        }

depending on the properties of the video, you can differentiate :

@Override Public voidOnvideosizechanged (MediaPlayer MP,intWidthintheight) {            if(Width = =0|| Height = =0) {log.e (TAG,"Invalid video width ("+ width +") or height ("+Height+")"); return; } logger.d (TAG,"onvideosizechanged Width:"+ width +"Height:"+height); Misvideosizeknown=true; Mvideoheight=height; Mvideowidth=width; intWID =mmediaplayer.getvideowidth (); intHIG =mmediaplayer.getvideoheight (); //Adjust the mode of display based on the properties of the video            if(Wid >hig) {                if(Getrequestedorientation ()! =Activityinfo.screen_orientation_landscape)                {setrequestedorientation (Activityinfo.screen_orientation_landscape); }            } Else {                if(Getrequestedorientation ()! =activityinfo.screen_orientation_portrait)                {setrequestedorientation (activityinfo.screen_orientation_portrait); }} displaymetrics DM=NewDisplaymetrics ();            Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); Msurfaceviewwidth=Dm.widthpixels; Msurfaceviewheight=Dm.heightpixels; if(Width >height) {                //Vertical screen recording of the video, adjust its upper and lower spare                intw = msurfaceviewheight * Width/height; intmargin = (msurfaceviewwidth-w)/2; LOGGER.D (TAG,"margin:"+margin); Relativelayout.layoutparams LP=NewRelativelayout.layoutparams (RelativeLayout.LayoutParams.MATCH_PARENT,                RelativeLayout.LayoutParams.MATCH_PARENT); Lp.setmargins (Margin,0, margin,0);            MSURFACEVIEW.SETLAYOUTPARAMS (LP); } Else {                //horizontal screen recording of the video, adjust its left and right to the spare                intH = msurfaceviewwidth * Height/width; intmargin = (msurfaceviewheight-h)/2; LOGGER.D (TAG,"margin:"+margin); Relativelayout.layoutparams LP=NewRelativelayout.layoutparams (RelativeLayout.LayoutParams.MATCH_PARENT,                RelativeLayout.LayoutParams.MATCH_PARENT); Lp.setmargins (0, margin,0, margin);            MSURFACEVIEW.SETLAYOUTPARAMS (LP); }        }

Android Surfaceview Adjust the screen when playing video

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.