zoukankan      html  css  js  c++  java
  • android SurfaceView中播放视频 按视频的原始比例播放

        OnPreparedListener mediaPlayerOnPreparedListener = new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer arg0) {
                // 首先取得video的宽和高
                int vWidth = mediaPlayer.getVideoWidth();
                int vHeight = mediaPlayer.getVideoHeight();
                
                // 该LinearLayout的父容器 android:orientation="vertical" 必须
                LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layoutPlay);
                int lw = linearLayout.getWidth();
                int lh = linearLayout.getHeight();
    
                if (vWidth > lw || vHeight > lh) {
                    // 如果video的宽或者高超出了当前屏幕的大小,则要进行缩放
                    float wRatio = (float) vWidth / (float) lw;
                    float hRatio = (float) vHeight / (float) lh;
    
                    // 选择大的一个进行缩放
                    float ratio = Math.max(wRatio, hRatio);
                    vWidth = (int) Math.ceil((float) vWidth / ratio);
                    vHeight = (int) Math.ceil((float) vHeight / ratio);
    
                    // 设置surfaceView的布局参数
                    LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(vWidth, vHeight);
                    lp.gravity = Gravity.CENTER;
                    surfaceView.setLayoutParams(lp);
                }
                // 然后开始播放视频
                mediaPlayer.start();
            }
        };
  • 相关阅读:
    装饰器的应用
    绑定路由关系
    基本使用
    numpy多项式拟合
    pandas空值处理与插值
    索引
    事务
    子查询
    视图
    自关联
  • 原文地址:https://www.cnblogs.com/bloodofhero/p/4920659.html
Copyright © 2011-2022 走看看