14

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 frames. So i have tried the following code for it

public void surfaceResize() {

   // WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Point size = new Point();
    int screenWidth = 0;
    //Get the SurfaceView layout parameters

    float aspectRatio = (float) recv_frame_width / recv_frame_height;

    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) 
    {   
        //Get the width of the screen
        getWindowManager().getDefaultDisplay().getSize(size);
        screenWidth = size.x;

        //Set the width of the SurfaceView to the width of the screen
        surflp.width = screenWidth;

        //Set the height of the SurfaceView to match the aspect ratio of the video 
        //be sure to cast these as floats otherwise the calculation will likely be 0
        surflp.height = (int) ((1 / aspectRatio) * (float)screenWidth);

        //Commit the layout parameters

    } else {

        size.x = size.y = 0;
        //Get the width of the screen
        getWindowManager().getDefaultDisplay().getSize(size);

        int screenHeight = size.y;

        //Set the width of the SurfaceView to the width of the screen
        surflp.height = screenHeight;

        //Set the width of the SurfaceView to match the aspect ratio of the video 
        //be sure to cast these as floats otherwise the calculation will likely be 0
        surflp.width = (int) ( aspectRatio * (float)screenHeight);

        //Commit the layout parameters
        // code to do for Portrait Mode        
    }
    surflp.addRule(RelativeLayout.CENTER_HORIZONTAL);
    surflp.addRule(RelativeLayout.CENTER_VERTICAL);

    if(myVideoSurfaceView != null) 
        myVideoSurfaceView.setLayoutParams(surflp);  
    System.out.println("Surface resized*****************************************");
}

Everything is fine if I call this function at beginning of call. My problem is when I call this function in between a call for changing aspect ratio it takes too much time to display the next frame. Sometimes the video gets stuck even.

I tried to destroy and recreate the surface with

myVideoSurface.setVisibility(VIEW.GONE);

But the surface is not getting created.

I am using Mediacodec for video decode I will get notified when there is a resolution change.

Is there something more I should do for resizing a surfaceView when already video is being played.

Thanks for help.........................

bland
  • 1,968
  • 1
  • 15
  • 22
Kevin K
  • 589
  • 2
  • 7
  • 28

2 Answers2

32

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);
        }
kyogs
  • 6,766
  • 1
  • 34
  • 50
  • Thanks kyogs for the fast response. But I too do something similar recv_frame_width and recv_frame_height are the current frame height and width. I want to know if there is something else to be done with the surfaceView for this change since the video is being played already. – Kevin K Dec 10 '13 at 10:18
  • from surface view you can get video height and width. – kyogs Dec 10 '13 at 10:23
  • @ kyogs Ya I know that but without that also I have the new width and height and the aspect ratio. but the problem comes when I try to resize my surfaceView, which takes too much time for it and sometimes the video gets stuck. – Kevin K Dec 10 '13 at 10:29
  • myVideoSurfaceView.setLayoutParams(surflp); This line is throwing some exception also... – Kevin K Dec 10 '13 at 12:10
  • @fadden can i have a help – Kevin K Dec 10 '13 at 12:10
  • android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. This is the exception that i get – Kevin K Dec 10 '13 at 13:10
  • 2
    You have to make calls that modify the View from the UI thread. You can use a Handler to "forward" the call. See e.g. https://developer.android.com/training/multiple-threads/communicate-ui.html . – fadden Dec 10 '13 at 16:21
  • Thank you fadden for the accurate solution – Kevin K Dec 11 '13 at 12:13
  • You save my day ! – meunicorn Oct 13 '16 at 08:31
  • @Fustigador pls share how u resolved screen aspect ratio – Erum May 25 '17 at 12:40
  • Doesn't work on my Android 9 device. Works on Android 6... – John Glen Jun 06 '21 at 00:46
3

enter image description here This problem can be solved like this.

Nasib
  • 1,173
  • 1
  • 13
  • 23