问题:我是用surfaceView+MediaPlay 播放本地上传的视频
但是由于视频尺寸不一致 有些视频播放会变形 (写个博客记录下开发时碰到的问题)
解决方案:视频尺寸会自动填充surfaceView,所以想要调整视频尺寸 修改surfaceView就好了。
通过监听mediaplayer的回调函数“onVideoSizeChanged” 在里面修改surfaceView的宽高
代码:
public void changeVideoSize() { int videoWidth = mediaPlayer.getVideoWidth(); int videoHeight = mediaPlayer.getVideoHeight(); //根据视频尺寸去计算->视频可以在sufaceView中放大的最大倍数。 float max; if (getResources().getConfiguration().orientation==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { //竖屏模式下按视频宽度计算放大倍数值 max = Math.max((float) videoWidth / (float) surfaceWidth,(float) videoHeight / (float) surfaceHeight); } else{ //横屏模式下按视频高度计算放大倍数值 max = Math.max(((float) videoWidth/(float) surfaceHeight),(float) videoHeight/(float) surfaceWidth); } //视频宽高分别/最大倍数值 计算出放大后的视频尺寸 videoWidth = (int) Math.ceil((float) videoWidth / max); videoHeight = (int) Math.ceil((float) videoHeight / max); //无法直接设置视频尺寸,将计算出的视频尺寸设置到surfaceView 让视频自动填充。 surfaceView.setLayoutParams(new RelativeLayout.LayoutParams(videoWidth, videoHeight)); }
@Override public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { changeVideoSize(); }
本文记录了使用SurfaceView+MediaPlayer播放本地视频时遇到的因视频尺寸不一致导致的变形问题。解决方法是通过监听MediaPlayer的"onVideoSizeChanged"回调,动态调整SurfaceView的宽高以匹配视频尺寸,确保视频正确填充而不失真。

1444

被折叠的 条评论
为什么被折叠?



