Resizing surface view for aspect ratio change in video display in android

前端 未结 2 1404
北恋
北恋 2020-12-31 08:02

I am working on a video conferencing project. My video display is using surface view. Now during a video call there is a chance of aspect ratio change for the incoming frame

相关标签:
2条回答
  • 2020-12-31 08:55

    Hello try with below code:

    private void setVideoSize() {
    
                // // Get the dimensions of the video
                int videoWidth = mediaPlayer.getVideoWidth();
                int videoHeight = mediaPlayer.getVideoHeight();
                float videoProportion = (float) videoWidth / (float) videoHeight;
    
                // Get the width of the screen
                int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
                int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
                float screenProportion = (float) screenWidth / (float) screenHeight;
    
                // Get the SurfaceView layout parameters
                android.view.ViewGroup.LayoutParams lp = surfaceView.getLayoutParams();
                if (videoProportion > screenProportion) {
                    lp.width = screenWidth;
                    lp.height = (int) ((float) screenWidth / videoProportion);
                } else {
                    lp.width = (int) (videoProportion * (float) screenHeight);
                    lp.height = screenHeight;
                }
                // Commit the layout parameters
                surfaceView.setLayoutParams(lp);
            }
    
    0 讨论(0)
  • 2020-12-31 08:56

    This problem can be solved like this.

    0 讨论(0)
提交回复
热议问题