android视频播放器Vitamio的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29654885/article/details/51899687

我一直想做个牛b的视频播放器 公司项目中也有涉及到。开始我想从网上找个写好的播放器,发现并不咋的,还是靠自己双手才行。我也是通过网上查资料的 所以有的代码可能会讲过。

首先 要实现横屏和竖屏的切换。当然是要重写onconfigchanged方法还要在清单文件中写上

android:configChanges="layoutDirection|screenSize|orientation"  这样当手机横竖屏切换的时候才会执行onconfigchanged方法。在这里要判断屏幕的方向到底是横屏还是竖屏。
 
 
super.onConfigurationChanged(newConfig);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    full(true);
} else {
    full(false);
}
当横屏的时候要设置状态栏是否隐藏,
private void full(boolean enable) {
    if (enable) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    } else {
        WindowManager.LayoutParams attr = getWindow().getAttributes();
        attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setAttributes(attr);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
}
当然好的播放器肯定会有手势操作,看看优酷搜狐的都很牛b,我们想实现这样的效果就要写手势操作,重写onscroll方法。
 
 
/**
 * 滑动
 */
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
                        float distanceX, float distanceY) {
    float mOldX = e1.getX(), mOldY = e1.getY();
    int y = (int) e2.getRawY();
    int x = (int) e2.getRawX();
    Display disp = getWindowManager().getDefaultDisplay();
    int windowWidth = disp.getWidth();
    int windowHeight = disp.getHeight();
    if (Math.abs(distanceX) >= Math.abs(distanceY)) {
        onSeekSlide((long) (distanceX / windowWidth * videoView.getDuration()));//快进或快退
    } else {
        if (mOldX > windowWidth * 4.0 / 5)// 右边滑动
            onVolumeSlide((mOldY - y) / windowHeight);
        else if (mOldX < windowWidth / 5.0)// 左边滑动
            onBrightnessSlide((mOldY - y) / windowHeight);

    }
    return super.onScroll(e1, e2, distanceX, distanceY);
}
全部代码见github:https://github.com/berlin2017/VitamioTest

猜你喜欢

转载自blog.csdn.net/qq_29654885/article/details/51899687