在android系统中的视频显示宽高比变化调整表面观 [英] Resizing surface view for aspect ratio change in video display in android
问题描述
我工作的一个视频会议项目。我的视频显示使用表面观。现在,在视频通话中有宽高比变化的输入帧的机会。所以,我曾尝试以下code吧
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.
我使用的媒体codeC的视频去code,我会得到通知时,有一个分辨率改变。
I am using Mediacodec for video decode I will get notified when there is a resolution change.
有没有更多的东西我应该做的调整一个surfaceView当视频已被播放。
Is there something more I should do for resizing a surfaceView when already video is being played.
感谢您的帮助..........
Thanks for help.........................
推荐答案
该问题的原因是意见的Android只能从它创建线程修改。但我想从另一个线程做到这一点。
现在我已经转发我的面观要求使用消息处理调整到UI线程和接收消息的UI线程会修改表面观...
The cause of the problem was views in android should only be modified from the thread where it is created. But I was trying do it from another thread.
Now I have forwarded my requirement of surface view resizing to the UI thread using a message handler and on receiving the message the ui thread will modify the surface view...
我在UI线程创建一个处理像
I created a handler in my UI thread like
Handler msg_hndlr = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch(msg.what) {
case 101:
surfaceResize();
break;
}
return false;
}
});
和从线程我想调用这个函数,我做了
And from the the thread I want to call this function, I did
msg_hndlr.sendEmptyMessage(101);
所以基本上我创建在UI线程的意见,并使用handler我让来自同一UI线程的表面即使在另一个线程的情况下发生的所有变化。
So basically I created the views in the UI thread and using the handler I make all changes for the surface from the same UI thread even if the case occured in another thread.
这篇关于在android系统中的视频显示宽高比变化调整表面观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!