VideoView

34 篇文章 0 订阅
1. VideoView使用小结

1. VideoView按原始视频比例播放


int screenH; // 手机屏幕高度

mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                        // 获取视频资源的宽度
                        mVideoWidth = mp.getVideoWidth();
                        // 获取视频资源的高度
                        mVideoHeight = mp.getVideoHeight();
                        scale = (float) mVideoWidth / (float) mVideoHeight;
                        refreshPortraitScreen(showVideoHeight == 0 ? screenH : showVideoHeight);
                    }
                });
               
            }
        });

    // 重新刷新 竖屏显示的大小  树屏显示以宽度为准
    public void refreshPortraitScreen(int height) {
        if (height == 0) {
            height = showVideoHeight;
        }
        if (mVideoHeight > 0 && mVideoWidth > 0) {
            // 拉伸比例
            mVideoWidth = (int) (height * scale);// 设置surfaceview画布大小
//            mVideoHeight = (int) (mVideoWidth / scale);
            mVideoView.getHolder().setFixedSize(mVideoWidth, height);
            // 重绘VideoView大小,这个方法是在重写VideoView时对外抛出方法
            mVideoView.setMeasure(mVideoWidth, height);
            mVideoView.requestLayout();
        }
    }
public class MyVideoView extends VideoView {

    private int width;
    private int height;
 
    public DiyVideoView(Context context) {
        super(context);
    }
 
    public DiyVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public void setMeasure(int width, int height) {
        this.width = width;
        this.height = height;
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		// 默认高度,为了自动获取到focus
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = width;
		// 这个之前是默认的拉伸图像
        if (this.width > 0 && this.height > 0) {
            width = this.width;
            height = this.height;
        }
        setMeasuredDimension(width, height);
    }
}

上面3小段即可实现视频适配播放

2. 参考
  1. andrioid videoview 视频自适应播放的问题
  2. 超简单 自定义VideoView 让视频拉伸至全屏
  3. android VideoView 获取当前播放时间、获取视频长度。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初心一点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值