仿抖音视频播放 基于GsyVideoPlayer+ViewPager2

前言

公司最近有个小的演示需求,大体首页模仿抖音,因此抽时间做了这么个项目,顺便练练手,demo用Kotlin+AndroidX实现。视频库采用
GsyVideoPlayer
话不多说,先上效果图
在这里插入图片描述

实现

实现方式ViewPager2(ViewPager2 其实就是RecyclerView + SnapHelper)

代码流程

  • 创建adapter并与ViewPager2绑定
  • 监听ViewPager2的PageChange,触发onPageSelect时进行视频播放
        viewPager2.registerOnPageChangeCallback(object : OnPageChangeCallback() {

            override fun onPageSelected(position: Int) {
                this@MainActivity.position = position
                super.onPageSelected(position)
                Log.i(AppConstants.TAG, "position --" + position + ":" + layoutManager.childCount)
                startVideo()
            }
        })
  • 继承GsyVideo的播放器,更改内部的ui流程,去除快进快退,其他ui可根据自己需求更改,这个库注释的很详细了。上一小段示例。下面这段目的就是去除拖动快进快退的功能。
    @Override
    protected void touchSurfaceDown(float x, float y) {
//        super.touchSurfaceDown(x, y);
        x1 = x;
        x2 = x;
        y1 = y;
        y2 = y;
        donwTime = System.currentTimeMillis();
    }
    @Override
    protected void touchSurfaceMove(float deltaX, float deltaY, float y) {
//        super.touchSurfaceMove(deltaX, deltaY, y);
        x2 = deltaX;
        y2 = deltaY;
    }
    @Override
    protected void touchSurfaceUp() {
        Log.i(TAG, "touchSurfaceUp");
//        super.touchSurfaceUp();
        if (System.currentTimeMillis() - donwTime < 500 && Math.abs(x2 - x1) < 20 && Math.abs(y2 - y1) < 20) {
            clickStartIcon();
        }

    }
  • 然后就没了,功能简单没啥好讲的,核心都是用的开源库,看了有时间还是得研究研究ffmpeg的编译。自己搞。

End

源代码地址已上传至github 点击前往
点击失效请复制: https://github.com/yudehai0204/DouYinImitation

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

猫的于

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

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

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

打赏作者

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

抵扣说明:

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

余额充值