仿抖音上下滑动切换视频

仿抖音上下滑动切换视频

    • 播放器配置
    • 滑动Activity

播放器配置

使用IjkVideoView
导入依赖

//导入IJKVideoView播放器
implementation 'com.dou361.ijkplayer:jjdxm-ijkplayer:1.0.5'

写布局item文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <com.dou361.ijkplayer.widget.IjkVideoView
        android:id="@+id/video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.dou361.ijkplayer.widget.IjkVideoView>


    <TextView
        android:layout_alignParentBottom="true"
        android:id="@+id/te_nowTiem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:text="00:00"
        android:textColor="#000" />

    <TextView
        android:layout_alignParentBottom="true"
        android:id="@+id/te_allTiem"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:text="00:00"
        android:textColor="#000" />

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />


    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OFF/ON" />

</RelativeLayout>

在适配器中使用

package com.example.pagersnaphelper;

import android.net.Uri;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dou361.ijkplayer.widget.IjkVideoView;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 使用万能适配器
 */
public class MyAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
    private Handler handler = new Handler();

    public MyAdapter(int layoutResId, @Nullable List<String> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        final IjkVideoView ijkVideoView = helper.getView(R.id.video);
        final Button button = helper.getView(R.id.start);
        final SeekBar seekBar = helper.getView(R.id.seekbar);
        final TextView allTime = helper.getView(R.id.te_allTiem);
        final TextView nowTime = helper.getView(R.id.te_nowTiem);

        ijkVideoView.setVideoURI(Uri.parse(item.toString()));
        ijkVideoView.start();

        //暂停/开始播放
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ijkVideoView.isPlaying()) {
                    ijkVideoView.pause();
                } else {
                    ijkVideoView.start();
                }
            }
        });

        //seekBar拖动进度条
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    int duration = ijkVideoView.getDuration();
                    int current = progress * duration / 100;
                    ijkVideoView.seekTo(current);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        //进度条自动滑动
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                final int currentPosition = ijkVideoView.getCurrentPosition();
                final int duration = ijkVideoView.getDuration();
                final int progress = currentPosition * 100 / duration;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        SimpleDateFormat format = new SimpleDateFormat("mm:ss");
                        seekBar.setProgress(progress);
                        String all_format = format.format(duration);
                        String now_format = format.format(currentPosition);
                        allTime.setText(all_format);
                        nowTime.setText(now_format);
                    }
                });
            }
        }, 0, 100);

    }

}

滑动Activity

使用基础的RecyclerView

XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview_vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</RelativeLayout>

Activity

package com.example.pagersnaphelper;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class PagerSnapHelperActivity extends Activity {

    /**
     * UI
     */
    // recycleView
    private RecyclerView mRecyclerView;
    // adapter
    private MyAdapter myAdapter;
    /**
     * 数据
     */
    //data
    private ArrayList<String> mDataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycle_pager_activity);

        // -----------创建数据集-------------
        initData();
        // 纵向List
        initUI();

    }

    //视频数据源
    private void initData() {
        mDataList.add("http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4");
        mDataList.add("http://vfx.mtime.cn/Video/2019/03/19/mp4/190319104618910544.mp4");
        mDataList.add("http://vfx.mtime.cn/Video/2019/03/19/mp4/190319125415785691.mp4");
        mDataList.add("http://vfx.mtime.cn/Video/2019/03/17/mp4/190317150237409904.mp4");
        mDataList.add("http://vfx.mtime.cn/Video/2019/03/14/mp4/190314223540373995.mp4");
        mDataList.add("http://vfx.mtime.cn/Video/2019/03/14/mp4/190314102306987969.mp4");
        mDataList.add("http://vfx.mtime.cn/Video/2019/03/13/mp4/190313094901111138.mp4");
        mDataList.add("http://vfx.mtime.cn/Video/2019/03/12/mp4/190312143927981075.mp4");
        mDataList.add("http://vfx.mtime.cn/Video/2019/03/12/mp4/190312083533415853.mp4");
        mDataList.add("http://vfx.mtime.cn/Video/2019/03/09/mp4/190309153658147087.mp4");
    }

    @SuppressLint("WrongConstant")
    public void initUI() {
        // ---RecyclerView---
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview_vertical);
        mRecyclerView.setNestedScrollingEnabled(false);
        // PagerSnapHelper
        PagerSnapHelper snapHelper = new PagerSnapHelper() {
            // 在 Adapter的 onBindViewHolder 之后执行
            @Override
            public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
                // TODO 找到对应的Index
                return super.findTargetSnapPosition(layoutManager, velocityX, velocityY);
            }

            // 在 Adapter的 onBindViewHolder 之后执行
            @Nullable
            @Override
            public View findSnapView(RecyclerView.LayoutManager layoutManager) {
                // TODO 找到对应的View
                return super.findSnapView(layoutManager);
            }
        };
        snapHelper.attachToRecyclerView(mRecyclerView);
        // ---布局管理器---
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        // 默认是Vertical (HORIZONTAL则为横向列表)
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //
        mRecyclerView.setLayoutManager(linearLayoutManager);

        myAdapter = new MyAdapter(R.layout.recycle_pager_item,mDataList);
        // 设置Adapter
        mRecyclerView.setAdapter(myAdapter);
    }
}

你可能感兴趣的:(仿抖音上下滑动切换视频)