Android SurfaceView内容居中显示 MediaPlayer视频比列适配


问题缘由:

最近准备封装一个视频播放器,我采用了SurfaceView + MediaPlayer的方式。

视频加载显示画面后遇到问题:“视频画面扭曲,比例变形。” 

问题截图:

解决思路:

视频尺寸会自动填充surfaceView,所以想要调整视频尺寸 修改surfaceView就好了。

通过监听mediaplayer的回调函数“onVideoSizeChanged” 在里面修改surfaceView的宽高。

changeVideoSize
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));
    }

注意:surfaceView的尺寸需要记录保存为固定值,否则在屏幕旋转后尺寸会变动。

 

效果截图:

完整代码:

 

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnVideoSizeChangedListener {

    private SurfaceView surfaceView;
    private MediaPlayer mediaPlayer;
    private int surfaceWidth;
    private int surfaceHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //初始化页面/对象/变量/监听事件
        setContentView(R.layout.activity_main);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceView.getHolder().addCallback(this);
    }

    private void playVideo(String url) {
        try {
            mediaPlayer.setDataSource(this, Uri.parse(url));
            mediaPlayer.prepare();
            mediaPlayer.start();} catch (IOException e) {e.printStackTrace();}
    }


    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnVideoSizeChangedListener(this);
        mediaPlayer.setDisplay(surfaceHolder);
      
if(getResources().getConfiguration().orientation==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
            surfaceWidth=surfaceView.getWidth();
            surfaceHeight=surfaceView.getHeight();
        }else {
            surfaceWidth=surfaceView.getHeight();
            surfaceHeight=surfaceView.getWidth();
        }

        playVideo("http://pgccdn.v.baidu.com/258500786_2854944371_20171201122626.mp4?authorization=bce-auth-v1%2Fc308a72e7b874edd9115e4614e1d62f6%2F2017-12-01T04%3A26%3A31Z%2F-1%2F%2Fcbe73c8f603a65a23a019236ad32c090ba08f587acae4011d388f153d912724f&responseCacheControl=max-age%3D8640000&responseExpires=Sun%2C+11+Mar+2018+12%3A26%3A31+GMT&xcode=0d70b526aeacd2423ca650defc16c18d96d564747874148f&time=1512191597&_=1512108201312");

    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        mediaPlayer.release();
        mediaPlayer=null;
    }

    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();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        changeVideoSize();
    }
}

 

最后,在此希望大家能够提供其它算法,应该会有更好的。

 

补充(2017-12-02):

一个最简单的解决方案(此方法仅用于surfaceView居中,不能适配视频比列):

 

@Override
    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
        surfaceView.setLayoutParams(new RelativeLayout.LayoutParams(width,height));
    }

 

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:gravity="center"
        android:background="#000">
        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>

在视频大小确定的回调函数中调整surfaceView尺寸,再通过layout嵌套surfaceView让其居中显示。

建议外层套RelativeLayout,用LinearLayout居中,画面显示不完整,这里可以自行测试。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM