Adjust the display ratio of surfaceView videos

Source: Internet
Author: User

Recently, I started to make a video player because I am interested in it. SurfaceView in full screen mode is used to play the video, but some videos can be played well while others are stretched. The video seems to be squashed. I can see an example of SurfaceView rewriting on the Internet. It seems like this is a method, but I have my own understanding of this problem: the cause of the problem is that the aspect ratio of SurfaceView is not the same as the aspect ratio of the video itself. So you can adjust the aspect ratio of SurfaceView Based on the aspect ratio of the video? Therefore, the following methods are available:

First, let's get the length and width of the mobile phone screen, because in full screen mode, the video height is the width of the mobile phone, all we need to do is to narrow down the SurfaceView width based on the video height:


  1. DisplayMetrics dm = new DisplayMetrics ();
  2. GetWindowManager (). getDefaultDisplay (). getMetrics (dm );
  3. MSurfaceViewWidth = dm. widthPixels;
  4. MSurfaceViewHeight = dm. heightPixels;

In the layout file, the length and width of SurfaceView are match_parent. The SurfaceView size at the beginning is equal to that of the mobile phone screen. Next, rewrite the onVideoSizeChanged method.
Here we can calculate the SurfaceView value based on the video size and screen width:


  1. @ Override
  2. Public void onVideoSizeChanged (MediaPlayer mp, int width, int height ){
  3. If (width = 0 | height = 0 ){
  4. Log. e (TAG, "invalid video width (" + width + ") or height (" + height
  5. + ")");
  6. Return;
  7. }
  8. Logger. d (TAG, "onVideoSizeChanged width:" + width + "height:" + height );
  9. MIsVideoSizeKnown = true;
  10. MVideoHeight = height;
  11. MVideoWidth = width;

  12. Int w = mSurfaceViewHeight * width/height;
  13. Int margin = (mSurfaceViewWidth-w)/2;
  14. Logger. d (TAG, "margin:" + margin );
  15. RelativeLayout. LayoutParams lp = new RelativeLayout. LayoutParams (
  16. RelativeLayout. LayoutParams. MATCH_PARENT,
  17. RelativeLayout. LayoutParams. MATCH_PARENT );
  18. Lp. setMargins (margin, 0, margin, 0 );
  19. MSurfaceView. setLayoutParams (lp );

  20. If (mIsVideoReadyToBePlayed & mIsVideoSizeKnown ){
  21. StartVideoPlayback ();
  22. }

  23. }

All right, experiment.







Related Article

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.