Question

I use a Frame Layout, on which i bind my video player like this:

private void initVideoPlayer(View root) {
    mVideoPlayer = new TextureViewVideoPlayer(width, height);
    mVideoPlayer.setOnErrorListener(this);
    mVideoPlayer.setOnPreparedListener(this);
    mVideoPlayer.setOnCompletionListener(this);
    mVideoPlayer.setOnBufferingUpdateListener(this);
    mVideoPlayer.setOnSeekCompleteListener(this);
    mVideoPlayer.setOnVideoSizeChangedListener(this);
    mVideoPlayer.bindView((FrameLayout) root.findViewById(R.id.videoFrame), 0);
}

This is the onVideoSizeChanged function, from the TextureViewVideoPlayer:

@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    Log.i(TAG, "onVideoSizeChanged " + width + " ZZ " + height + " Display Height: " + displayheight + " --- REAL VIDEO RATIO: -- " + ((double) width / (double) height));
    if (this.mOnVideoSizeChangedListener != null)
        this.mOnVideoSizeChangedListener.onVideoSizeChanged(this, width, height);
    if (width == 0 || height == 0) {
        mp.release();
        Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
        return;
    }
    double ratio = ((double) width / (double) height);
    if (displayheight > height) {
        ratio = ((double) displayheight / (double) height);
    } else {
        ratio = ((double) height / (double) displayheight);
    }
    LayoutParams params = new FrameLayout.LayoutParams((int) (width * ratio), (int) (height * ratio));
    // LayoutParams params = new FrameLayout.LayoutParams((width),
    // (height));
    Log.i(TAG, "onVideoSizeChanged mod" + params.width + " ZZ " + params.height + " RATIO: " + ratio + " --- REAL RATIO: -- " + ((double) params.width / (double) params.height));
    params.gravity = Gravity.CENTER;
    mTextureView.setLayoutParams(params);
    this.mTextureView.requestLayout();
}

As you can see, it takes the video width and height of the video, it calculates the ratio, then it takes the displays height, and makes a ratio to see how much bigger (or smaller) the video is than the screen, then it creates the params for the video, with that ratio, and sets this parameters on the textureView.

In the xml, the VideoFrame i bind my player too has this:

<FrameLayout
    android:id="@+id/videoFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >
</FrameLayout>

Now, if I keep it like this, the video is stretched, and even though it looks like its a rectangle, a part from the top and bottom of the video disappear (being 1:1 the width and height are the same, and the height is bigger that the screen). Now, if I don't use the ratio, and create the parameters with the width and height of the video (640 x 480) It will be a small square. (ratio 1:1), and stretched. Any ideea how this could be avoided?

PS: My texture view in fact is a Square Texture View, which had this onMeasure function:

 @Override
protected void onMeasure(int paramInt1, int paramInt2) {
    int i = View.MeasureSpec.getSize(paramInt1);
    setMeasuredDimension(i, i);
}

I've changed it into:

  @Override
protected void onMeasure(int paramInt1, int paramInt2) {
    int i = View.MeasureSpec.getSize(paramInt1);
    int j = View.MeasureSpec.getSize(paramInt2);
    setMeasuredDimension(i, j);
}

Hopefully, this fixes my issues.

Was it helpful?

Solution

MY texture view had this:

 @Override
protected void onMeasure(int paramInt1, int paramInt2) {
int i = View.MeasureSpec.getSize(paramInt1);
setMeasuredDimension(i, i);

}

To fix it, I've changed it into:

@Override
protected void onMeasure(int paramInt1, int paramInt2) {
int i = View.MeasureSpec.getSize(paramInt1);
int j = View.MeasureSpec.getSize(paramInt2);
setMeasuredDimension(i, j);

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top