使用VideoView做个实用的视频播放器
目录 最终效果图 前言 布局文件 VideoView的使用 横竖屏切换 文件选择 手势调节音量 最后 最终效果图 最终效果图 前言 这里用VideoView写一个播放器, 可以横竖屏, 可以选文件, 可以暂停, 可以快进后退, 可以进度条拖动, 可以触屏调节音量. 来看看怎么实现的吧! 布局文件 用RelativeLayout包裹VideoView是要点, 常规设置会形变的. 当然了, 还要重写onConfigurationChanged, 见后面横竖屏切换. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.so.mymedia.ui.activity.MainActivity" tools:showIn="@layout/activity_main"> <RelativeLayout android:id="@+id/rl_vv" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/black" android:minHeight="200dp"> <VideoView android:id="@+id/vv_video" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout> </RelativeLayout> VideoView的使用 VideoView使用起来非常简单, 设置好MediaController, 然后设置URI或者是Path, 然后start开始就好. 这里的要点是一些使用功能的实现. 可以查阅官方文档. 官方文档 横竖屏切换 第一步是到配置文件里面设置. 在activity标签下添加android:configChanges="keyboard|orientation|screenSize". 这样的话, 屏幕切换的时候不会去调用onStop等方法. 我们在Toolbar里面添加切换横竖屏按钮, 然后重写onConfigurationChanged. @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (mVvVideo == null) { return; } if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { // 横屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().getDecorView().invalidate(); float height = new ScreenUtil(this).getAppWidth(); float width = new ScreenUtil(this).getAppHeight(); mRlVv.getLayoutParams().height = (int) width; mRlVv.getLayoutParams().width = (int) height; } else { // 竖屏 final WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().setAttributes(attrs); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); float width = new ScreenUtil(this).getAppWidth(); float height = DisplayUtil.dp2px(this, 200.f); mRlVv.getLayoutParams().height = (int) height; mRlVv.getLayoutParams().width = (int) width; } } 里面有几个uitl, 都是常见的封装, 不多说了. 这样就可以实现横竖屏切换了. 文件选择 关于文件选择器, 请查看我之前的文章. 然后就是要返回选中的文件路径. 这是Intent的常规使用了. 不多说了. 手势调节音量 添加触摸监听, 然后用手势操作实现. 然后是依据上下划方向确定增大还是减小音量. 调节音量的代码也是很常规的了. @Override public boolean onTouch(View v, MotionEvent event) { return mGestureDetector.onTouchEvent(event); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { float v = e2.getY() - e1.getY(); if (Math.abs(v) > 10) { setVoiceVolume(v); } return true; } private void setVoiceVolume(float value) { int currentVolume = mAM.getStreamVolume(AudioManager.STREAM_MUSIC); int maxVolume = mAM.getStreamMaxVolume(AudioManager.STREAM_MUSIC); int flag = value > 0 ? -1 : 1; currentVolume += flag * 0.15 * maxVolume; mAM.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0); } 最后 如果想更多的DIY, 可以考虑使用SurfaceView, 但是VideoView大部分时候也够用了. 喜欢记得点赞或者关注我, 有意见或者建议评论区见.