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