Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调整android视频显示中纵横比变化的表面视图大小_Android_Video_Surfaceview_Android Mediacodec - Fatal编程技术网

调整android视频显示中纵横比变化的表面视图大小

调整android视频显示中纵横比变化的表面视图大小,android,video,surfaceview,android-mediacodec,Android,Video,Surfaceview,Android Mediacodec,我正在做一个视频会议项目。我的视频显示使用surface view。现在,在视频通话过程中,传入帧的纵横比可能会发生变化。因此,我尝试了以下代码 public void surfaceResize() { // WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Point size = new Point(); int screenWidth = 0;

我正在做一个视频会议项目。我的视频显示使用surface view。现在,在视频通话过程中,传入帧的纵横比可能会发生变化。因此,我尝试了以下代码

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*****************************************");
}
如果我在调用开始时调用这个函数,一切都很好。 我的问题是,当我在调用更改纵横比之间调用此函数时,显示下一帧需要花费太多时间。有时视频甚至会被卡住

我试图用它来破坏和重建表面

myVideoSurface.setVisibility(VIEW.GONE);
但表面并没有被创造出来

我正在使用Mediacodec进行视频解码,当分辨率发生变化时,我会得到通知

当已经在播放视频时,我是否还应该做些什么来调整surfaceView的大小


感谢您的帮助………

您好,请尝试以下代码:

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的快速响应。但是我也做了一些类似的事情,recv_frame_width和recv_frame_height是当前的帧高度和宽度。我想知道,由于视频已经在播放,是否需要对surfaceView进行其他更改。从surface view可以获得视频的高度和宽度。@kyogs Ya我知道,但如果没有它,我也有新的宽度和高度以及纵横比。但是当我尝试调整surfaceView的大小时,问题就出现了,这会花费太多的时间,有时视频会被卡住;这一行也引发了一些异常…您必须从UI线程进行修改视图的调用。您可以使用处理程序“转发”调用。如果您的问题得到解决,请接受回答。所以你的问题不会出现在“未回答”列表中