surfaceview播放视频被拉伸的处理

用手机竖屏拍的视频,在横屏的机器上播放时,会被拉的很扁,开始以为是横竖屏的原因,后来发现横屏播放也不行。于是做了点处理。

如果是用mediaplay的异步prepare方法,onPrepare方法中添加以下代码:

int videoWidth = mediaPlayer.getVideoWidth();
int videoHeight = mediaPlayer.getVideoHeight();

DisplayMetrics dm = new DisplayMetrics();
((MovieVideoActivity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
int mSurfaceViewWidth = dm.widthPixels;
int mSurfaceViewHeight = dm.heightPixels;

int w = mSurfaceViewHeight * videoWidth / videoHeight;
int margin = (mSurfaceViewWidth - w) / 2; 
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
lp.setMargins(margin, 0, margin, 0);
mSurfaceView.setLayoutParams(lp);
如果是用同步的prepare,则在prepare之后添加

你可能感兴趣的:(surfaceview播放视频被拉伸的处理)