GlSurfaceView setZMediaOrder makes the video play outside of its parents
Case-1:
GLSurfaceView | FrameLayout | TextureView | VideoRecording
So, I have GLSurfaceView
one based on user input. I add a square above it FrameLayout
(dimensions are set using layoutparams) and then add TextureView
on top of this newly added one FrameLayout
and then record the video as expected, no matter which camera I choose, I get a square view.
Case-2:
GLSurfaceView | FrameLayout | GLSurfaceView | VideoPlayback
In case -1, after recording a video, I add another one GLSurfaceView
, removing the previous one TextureView
from the already added one FrameLayout
. Previously, I could just hear the playback sound without any video, but later I came across examples that used setZOrderMediaOverlay(true);
or setZOrderOnTop(true)
Now the video plays, but instead of playing inside its parent, it plays in 9/16 aspect ratio. As mentioned earlier, the expected behavior is a square video (FrameLayout dimensions).
How can I get a newly added video on top of a background video without leaving its parent's limits.
source to share
I think you need to check the aspect ratio of the media player and set accordingly, use relative layout instead of frame layout and set the center of gravity to CENTER and implement this code:
MediaPlayer.OnVideoSizeChangedListener mOnVideoSizeChangedListener = new MediaPlayer.OnVideoSizeChangedListener() {
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
setAspectRatio(mp, width, height);
}
};
And create a method and set the video layout parameters according to this:
private void setAspectRatio(MediaPlayer mediaPlayer, int videoWidth, int videoHeight)
{
if(mediaPlayer != null)
{
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
ViewGroup.LayoutParams videoParams = getLayoutParams();
if (videoWidth > videoHeight)
{
videoParams.width = width;
videoParams.height = width * videoHeight / videoWidth;
}
else
{
videoParams.width = height * videoWidth / videoHeight;
videoParams.height = height;
}
// Commit params
surfaceView.setLayoutParams(videoParams);
}
}
source to share