gpt4 book ai didi

android - 为 android 中视频显示的纵横比变化调整表面 View

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:53:44 26 4
gpt4 key购买 nike

我正在从事一个视频 session 项目。我的视频显示使用表面 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);
}

关于android - 为 android 中视频显示的纵横比变化调整表面 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20491499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com