我经历过当 VideoView 的维度为 0 时,视频不会播放。确保设置初始尺寸。
此外,我会尝试从一开始就设置布局:
videoView.setVideoLayout(VideoView.VIDEO_LAYOUT_SCALE, 0);
我会在收到 oncVideoSizeChanged 回调后设置尺寸,例如,
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
FrameLayout.LayoutParams vLayout = (FrameLayout.LayoutParams) mSurfaceView.getLayoutParams();
vLayout.width = getWindowManager().getDefaultDisplay().getWidth();
vLayout.height = getWindowManager().getDefaultDisplay().getHeight();
float aspectRatio = (float) width / height;
float screenRatio = (float) vLayout.width / vLayout.height;
float topMargin = 0, leftMargin = 0;
if (screenRatio < aspectRatio)
topMargin = (float) vLayout.height
- ((float) vLayout.width / aspectRatio);
else if (screenRatio > aspectRatio)
leftMargin = (float) vLayout.width - (vLayout.height * aspectRatio);
vLayout.setMargins((int) leftMargin, (int) topMargin, 0, 0);
mSurfaceView.setLayoutParams(vLayout);
}