您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页使用VideoView做个实用的视频播放器

使用VideoView做个实用的视频播放器

来源:二三娱乐

目录

  • 最终效果图
  • 前言
  • 布局文件
  • VideoView的使用
  • 横竖屏切换
  • 文件选择
  • 手势调节音量
  • 最后

最终效果图

最终效果图

前言

这里用VideoView写一个播放器, 可以横竖屏, 可以选文件, 可以暂停, 可以快进后退, 可以进度条拖动, 可以触屏调节音量. 来看看怎么实现的吧!


布局文件

RelativeLayout包裹VideoView是要点, 常规设置会形变的. 当然了, 还要重写onConfigurationChanged, 见后面横竖屏切换.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    
    
    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的使用

官方文档

横竖屏切换

第一步是到配置文件里面设置. 在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, 都是常见的封装, 不多说了. 这样就可以实现横竖屏切换了.


文件选择


手势调节音量

添加触摸监听, 然后用手势操作实现. 然后是依据上下划方向确定增大还是减小音量. 调节音量的代码也是很常规的了.

@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大部分时候也够用了. 喜欢记得点赞或者关注我, 有意见或者建议评论区见.


Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务