Android音视频开发之VideoView使用指南

 更新时间:2022年04月12日 09:08:26   作者:JulyYu  
VideoView组件内部同样是使用MediaPlayer+SurfaceView的形式控制MediaPlayer对视频文件进行播放,本文就来详细讲讲它的使用方法,需要的可以参考一下

VideoView介绍

之前介绍过使用MediaPlayer+SurfaceView实现播放视频功能。无意间发现官方封装了VideoView组件来实现简单视频播放功能,内部同样是使用MediaPlayer+SurfaceView的形式控制MediaPlayer对视频文件进行播放。使用场景比较简单,适用于只是播放视频的场景,其提供能力有限不太适合使用在调节视频亮度等其他功能。

MediaController

除了播放组件VideoView外还有MediaController组件为视频播放提供播放操作栏功能,可支持视频播放、暂停、快进、快退等功能。另外还提供进度条功能可以拖拽到指定位置进行播放视频。

使用

VideoView封装了MediaPlayer同样也提供了类似于MediaPlayer的api。例如start方法同样是播放视频功能,但调用该方法前最好也是通过设置setOnpreparedListener回调结果来执行,当调用setVideoPath后会主动执行prepareAsync方法。在VideoView内部帮助开发者封装实现了很多功能,其实也能借鉴其内部源码来实现功能更全面功能更完备的自制播放器。

常用Api说明
setVideoPath设置视频资源
start播放
pause暂停
resume重播
seekTo指定位置播放
isPlaying视频是否播放
getCurrentPosition获取当前播放位置
setMediaController设置MediaController
setOnpreparedListener监听视频装载完成事件
// 实例化videoView     
videoView = new VideoView(this);
Uri uri = Uri.fromFile(new File("sdcard/DCIM","新世纪福音战士24.mp4"));
//加载视频资源
videoView.setVideoURI(uri);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.addView(videoView);
setContentView(linearLayout);
//设置监听
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        //回调成功并播放视频
        videoView.start();
    }
});
//创建操作栏
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
mediaController.setMediaPlayer(videoView);

源码分析

既然封装了VideoViewMediaController两者组件,在使用过程中也发现了许多之前尝试实现的一些功能看看他们又是如何实现的。

进度显示

MediaController显示时调用show方法内部可以看到一个post(mShowProgress);方法

public void show(int timeout) {
    if (!mShowing && mAnchor != null) {
        setProgress();
        if (mPauseButton != null) {
            mPauseButton.requestFocus();
        }
        disableUnsupportedButtons();
        updateFloatingWindowLayout();
        mWindowManager.addView(mDecor, mDecorLayoutParams);
        mShowing = true;
    }
    updatePausePlay();

    // cause the progress bar to be updated even if mShowing
    // was already true.  This happens, for example, if we're
    // paused with the progress bar showing the user hits play.
    post(mShowProgress);

    if (timeout != 0 && !mAccessibilityManager.isTouchExplorationEnabled()) {
        removeCallbacks(mFadeOut);
        postDelayed(mFadeOut, timeout);
    }
}

可以看到mShowProgress是一个Runnable,内部会延迟不停调用自己来更新setProgress()setProgress()方法就是读取MediaPlayer播放进度从而更新播放信息。

private final Runnable mShowProgress = new Runnable() {
    @Override
    public void run() {
        int pos = setProgress();
        if (!mDragging && mShowing && mPlayer.isPlaying()) {
            postDelayed(mShowProgress, 1000 - (pos % 1000));
        }
    }
};

播放尺寸适配

之前自定义实现播放尺寸适配,在VideoView内部直接帮助开发者实现视频播放适配,详细代码可以直接看onMeasure重写。代码大致算法就是通过比较VideoView布局宽高和视频的宽高进行比例比较来重写计算VideoView的宽高。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //Log.i("@@@@", "onMeasure(" + MeasureSpec.toString(widthMeasureSpec) + ", "
    //        + MeasureSpec.toString(heightMeasureSpec) + ")");

    int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
    int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
    if (mVideoWidth > 0 && mVideoHeight > 0) {

        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthSpecMode == MeasureSpec.EXACTLY && heightSpecMode == MeasureSpec.EXACTLY) {
            // the size is fixed
            width = widthSpecSize;
            height = heightSpecSize;

            // for compatibility, we adjust size based on aspect ratio
            if ( mVideoWidth * height  < width * mVideoHeight ) {
                //Log.i("@@@", "image too wide, correcting");
                width = height * mVideoWidth / mVideoHeight;
            } else if ( mVideoWidth * height  > width * mVideoHeight ) {
                //Log.i("@@@", "image too tall, correcting");
                height = width * mVideoHeight / mVideoWidth;
            }
        } else if (widthSpecMode == MeasureSpec.EXACTLY) {
            // only the width is fixed, adjust the height to match aspect ratio if possible
            width = widthSpecSize;
            height = width * mVideoHeight / mVideoWidth;
            if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
                // couldn't match aspect ratio within the constraints
                height = heightSpecSize;
            }
        } else if (heightSpecMode == MeasureSpec.EXACTLY) {
            // only the height is fixed, adjust the width to match aspect ratio if possible
            height = heightSpecSize;
            width = height * mVideoWidth / mVideoHeight;
            if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
                // couldn't match aspect ratio within the constraints
                width = widthSpecSize;
            }
        } else {
            // neither the width nor the height are fixed, try to use actual video size
            width = mVideoWidth;
            height = mVideoHeight;
            if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
                // too tall, decrease both width and height
                height = heightSpecSize;
                width = height * mVideoWidth / mVideoHeight;
            }
            if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
                // too wide, decrease both width and height
                width = widthSpecSize;
                height = width * mVideoHeight / mVideoWidth;
            }
        }
    } else {
        // no size yet, just adopt the given spec sizes
    }
    setMeasuredDimension(width, height);
}

到此这篇关于Android音视频开发之VideoView使用指南的文章就介绍到这了,更多相关Android VideoView内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android控件系列之Toast使用介绍

    Android控件系列之Toast使用介绍

    Toast可以说是最常用也是最简单的Android控件之一,其自动关闭的功能大大简化了代码量,不失为用户提示的最佳选择
    2012-11-11
  • Android实现歌词渐变色和进度的效果

    Android实现歌词渐变色和进度的效果

    这篇文章主要介绍了Android实现歌词渐变色和进度的效果的相关资料,需要的朋友可以参考下
    2016-03-03
  • Android原生集成RN最新版教程

    Android原生集成RN最新版教程

    这篇文章主要为大家介绍了Android原生集成RN最新版教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Flutter深色模式适配的实现

    Flutter深色模式适配的实现

    这篇文章主要介绍了Flutter深色模式适配的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Flutter 异步编程之单线程下异步模型图文示例详解

    Flutter 异步编程之单线程下异步模型图文示例详解

    这篇文章主要为大家介绍了Flutter 异步编程之单线程下异步模型图文示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Android scrollview如何监听滑动状态

    Android scrollview如何监听滑动状态

    这篇文章主要介绍了Android scrollview监听滑动状态的实例代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • Android WebView实现顶部进度条

    Android WebView实现顶部进度条

    这篇文章主要为大家详细介绍了Android WebView实现顶部进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Android实现日夜间模式的深入理解

    Android实现日夜间模式的深入理解

    相信Android的日间/夜间模式切换相信大家在平时使用 APP 的过程中都遇到过,比如知乎、简书中就有相关的模式切换。实现日间/夜间模式切换的方案也有许多种,趁着今天有空来讲一下日间/夜间模式切换的几种实现方案,也可以做一个横向的对比来看看哪种方案最好。
    2016-09-09
  • Android入门之Toast的使用教程

    Android入门之Toast的使用教程

    Toast是一种很方便的消息提示框,会在 屏幕中显示一个消息提示框,没任何按钮,也不会获得焦点一段时间过后自动消失!非常常用!本文就来通过一个例子把Toast的使用讲透
    2022-11-11
  • Android开发之判断有无虚拟按键(导航栏)的实例

    Android开发之判断有无虚拟按键(导航栏)的实例

    下面小编就为大家分享一篇Android开发之判断有无虚拟按键(导航栏)的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01

最新评论