Make writing a habit together! This is the third day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

preface

There is also a problem to be solved when using MediaPlayer’s ability to play video, and that is the fit size. By default, playing a video using the SurfaceView may cause the video to be stretched or compressed.

MediaPlayer internal solution

The video resource information is shown below. The size of the video is 1080×606, and the width is larger than the height.

Normal, Video information

MediaPlayer supports video playback size Settings: The VIDEO_SCALING_MODE_SCALE_TO_FIT and VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING are set in the setVideoScalingMode method. The two ways of display are shown in the figure below.

VIDEO_SCALING_MODE_SCALE_TO_FIT VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING

However, in order to achieve the desired effect in the actual display, the size of the window must be modified in order to achieve the preset size adaptation.

Adaptation scheme

The algorithm design in the adaptation scheme should be that one side of the video width and height fills the screen (because other cases are not considered for the time being), and the screen size should be used as the benchmark by default. Let’s take portrait screen as an example. Landscape is the same thing.

Pre-designed for vertical screen to be full width, calculate the screen width to height ratio, use the screen width to height ratio after obtaining the video size. Take the width of the screen as the width of the video size after scaling, and then calculate the height of the video size after scaling. The final calculated size is not the SurfaceView view window size so that you can ensure that the playback video size is stretched.

Code section

The video size information is available after MediaPlayer is ready to load the video resource. Remember that you can’t get the video size via getTrackInfo(), and you don’t need MediaExtractor. You can get the video information directly through MediaPlayer when it is ready.

@Override public void onPrepared(MediaPlayer mp) { playerInfo.playerStatus = PREPARED; Int width = mp.getVideowidth (); int height = mp.getVideoHeight(); playerInfo.videoSize = new Size(width,height); callbackInfo(playerInfo); }Copy the code

After the resource is ready, the screen size is obtained in the callback, and the algorithm calculates the window size to achieve the video size adaptation.

@Override public void onPlayerInfoCallBack(AndroidMediaPlayer.PlayerInfo playerInfo) { if(playerInfo.getPlayerStatus() = = AndroidMediaPlayer. PREPARED) {/ / screen Size proportion Size = ScreenUtils. GetScreenSize (TestSimpleMediaPlayerActivity. This); // Size videoSize = playerinfo.getVideoSize (); // Float videoRatio = videosize.getwidth ()/(float) videosize.getheight (); float newHeight = size.getWidth() / videoRatio; LinearLayout.LayoutParams layoutParams =(LinearLayout.LayoutParams) surfaceView.getLayoutParams(); layoutParams.height = (int)newHeight; / / reset window size high surfaceView. SetLayoutParams (layoutParams); androidMediaPlayer.start(); }}Copy the code

conclusion

In fact, the size of the video playing window can be set by changing the size of the final playing window. Of course, you can not adapt the window size according to the screen size as a benchmark, such as SurfaceView external layout window is a square, but the display effect may not be very good. Secondly, if the video size is to be scaled and clipped internally (16:9, 3:4), other schemes may be needed.