VideoVideo - Video Width x Height- MediaPlayer Issue

自作多情 提交于 2019-12-03 16:03:07
Blundell

So this issue occurs with VideoView and with MediaPlayer + SurfaceView.

When the system asks the video for it's width x height it is returning 640x480 when it should be returning 852x480.

i.e.

@Override
public void onPrepared(MediaPlayer mp) {
    mp.getVideoWidth();
    mp.getVideoHeight();
}

This is either a bug in the MediaPlayer handling of the video container/codec or an issue with the video file itself.

Either way I have circumvented it by adding what I know the video's width and height is to my code. I have updated the Git Repo with this fix. hack alert

Here's a link to the question where I found out the different size details of my video.

You can apply this fix to the VideoView or directly to MediaPlayer + SurfaceView your choice. Here's the VideoView answer:

public class FitVideoView extends VideoView {

    private final int mVideoWidth = 853;
    private final int mVideoHeight = 480;
    private boolean applyFix = true;

    public FitVideoView(Context context) {
        super(context);
    }

    public FitVideoView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FitVideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (applyFix) { // A Toggle so I can see both results
            // This doesn't ask the video for it's size, but uses my hardcoded size
            applyFix(widthMeasureSpec, heightMeasureSpec);
        } else {
            // This asks the video for its size (which gives an incorrect WxH) then does the same code as below
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    private void applyFix(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
        int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
        if (mVideoWidth > 0 && mVideoHeight > 0) {
            if (mVideoWidth * height > width * mVideoHeight) {
                Log.d("TAG", "image too tall, correcting");
                height = width * mVideoHeight / mVideoWidth;
            } else if (mVideoWidth * height < width * mVideoHeight) {
                Log.d("TAG", "image too wide, correcting");
                width = height * mVideoWidth / mVideoHeight;
            } else {
                Log.d("TAG", "aspect ratio is correct: " + width + "/" + height + "=" + mVideoWidth + "/" + mVideoHeight);
            }
        }
        Log.d("TAG", "setting size: " + width + 'x' + height);
        setMeasuredDimension(width, height);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!