Android 基于dkPlayer实现抖音列表播放

Android 基于dkPlayer实现抖音列表播放

记录一下实现抖音列表播放的过程,虽然最后项目叫停了,不过还是记录一下开发的辛酸。
先看下效果图吧,(这个妹子还是挺好看的)
主要说下列表播放的实现吧!

1、导入dkplayer。
dkplayer-armv7a
dkplayer-java
dkplayer-ui

#2、重写控制器,实现暂停,播放,点赞,评论,分享,等等功能

/**
 * 抖音
 * Created by Ryan 2019/6/5
 */

public class TikTokController extends GestureVideoController implements View.OnClickListener{

    private ImageView thumb;
    private ImageView iv_play;
    private LinearLayout comment_ll;

    public TikTokController(@NonNull Context context) {
        super(context);
    }

    public TikTokController(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TikTokController(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_tiktok_controller;
    }

    public void setPlayViewStatus(boolean isPause){
        if (isPause){
            iv_play.setVisibility(VISIBLE);
        }else {
            iv_play.setVisibility(GONE);
        }
    }

    @Override
    public void show() {
        if (!mShowing) {
            if (mMediaPlayer.isFullScreen()) {
                if (!mIsLocked) {
                    showAllViews();
                }
            } else {
                iv_play.setVisibility(View.VISIBLE);
                doPauseResume();
            }
            mShowing = true;
        }
        removeCallbacks(mFadeOut);
    }

    private void showAllViews() {
        iv_play.setVisibility(View.VISIBLE);
        doPauseResume();
    }


    @Override
    protected void initView() {
        super.initView();
        thumb = mControllerView.findViewById(R.id.iv_thumb);
        iv_play = mControllerView.findViewById(R.id.iv_play);
        comment_ll = mControllerView.findViewById(R.id.comment_ll);
        iv_play.setOnClickListener(this);
        comment_ll.setOnClickListener(this);
    }

    protected void doPauseResume() {
        if (mCurrentPlayState == IjkVideoView.STATE_BUFFERING) return;
        if (mMediaPlayer.isPlaying()) {
            iv_play.setSelected(false);
            iv_play.setVisibility(VISIBLE);
            mMediaPlayer.pause();
        } else {
            mMediaPlayer.start();
            iv_play.setSelected(true);
            iv_play.setVisibility(GONE);
        }
    }

    @Override
    public void setPlayState(int playState) {
        super.setPlayState(playState);
        switch (playState) {
            case IjkVideoView.STATE_IDLE:
                L.e("STATE_IDLE");
                thumb.setVisibility(VISIBLE);
                break;
            case IjkVideoView.STATE_PLAYING:
                L.e("STATE_PLAYING");
                thumb.setVisibility(GONE);
                break;
            case IjkVideoView.STATE_PREPARED:
                L.e("STATE_PREPARED");
                break;
        }
    }

    public ImageView getThumb() {
        return thumb;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_thumb || v.getId() == R.id.iv_play){
            doPauseResume();
        }else if (v.getId() == R.id.comment_ll){
            Log.e("ryan","点击了");
        }
    }

    @Override
    public void hide() {
        if (mShowing) {
            if (mMediaPlayer.isFullScreen()) {
                if (!mIsLocked) {
                    hideAllViews();
                }
            } else {
                iv_play.setVisibility(View.GONE);
                doPauseResume();
            }
            mShowing = false;
        }
    }

    private void hideAllViews() {
        iv_play.setVisibility(View.GONE);
        doPauseResume();
    }
}

#3、实现播放器

public class TikTokActivity extends AppCompatActivity {

    private static final String TAG = "TikTokActivity";
    private IjkVideoView mIjkVideoView;
    private TikTokController mTikTokController;
//    private StandardVideoController mTikTokController;
    private int mCurrentPosition;
    private RecyclerView mRecyclerView;
    private List<VideoBean> mVideoList;
    private     TikTokAdapter tikTokAdapter;
    boolean isPause;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("");
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        setContentView(R.layout.activity_tiktok);

        setStatusBarTransparent();

        mIjkVideoView = new IjkVideoView(this);
        mIjkVideoView.setLooping(true);
        mTikTokController = new TikTokController(this);
//        mTikTokController = new StandardVideoController(this);
        mIjkVideoView.setVideoController(mTikTokController);
        mRecyclerView = findViewById(R.id.rv);

        mVideoList = DataUtil.getTikTokVideoList();
        tikTokAdapter = new TikTokAdapter(mVideoList, this);
        ViewPagerLayoutManager layoutManager = new ViewPagerLayoutManager(this, OrientationHelper.VERTICAL);

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(tikTokAdapter);
        layoutManager.setOnViewPagerListener(new OnViewPagerListener() {
            @Override
            public void onInitComplete() {
                //自动播放第一条
                startPlay(0);
            }


            @Override
            public void onPageRelease(boolean isNext, int position) {
                if (mCurrentPosition == position) {
                    mIjkVideoView.release();
                }
            }

            @Override
            public void onPageSelected(int position, boolean isBottom) {
                if (mCurrentPosition == position) return;
                startPlay(position);
                mCurrentPosition = position;
                mTikTokController.setPlayViewStatus(false);
            }
        });
    }

    private void startPlay(int position) {
        View itemView = mRecyclerView.getChildAt(0);
        FrameLayout frameLayout = itemView.findViewById(R.id.container);
        Glide.with(this)
                .load(mVideoList.get(position).getThumb())
                .placeholder(android.R.color.white)
                .into(mTikTokController.getThumb());
        ViewParent parent = mIjkVideoView.getParent();
        if (parent instanceof FrameLayout) {
            ((FrameLayout) parent).removeView(mIjkVideoView);
        }
        frameLayout.addView(mIjkVideoView);
        mIjkVideoView.setUrl(mVideoList.get(position).getUrl());
        mIjkVideoView.setScreenScale(IjkVideoView.SCREEN_SCALE_CENTER_CROP);
        mIjkVideoView.start();
    }

    /**
     * 把状态栏设成透明
     */
    private void setStatusBarTransparent() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = TikTokActivity.this.getWindow().getDecorView();
            decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
                    WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
                    return defaultInsets.replaceSystemWindowInsets(
                            defaultInsets.getSystemWindowInsetLeft(),
                            0,
                            defaultInsets.getSystemWindowInsetRight(),
                            defaultInsets.getSystemWindowInsetBottom());
                }
            });
            ViewCompat.requestApplyInsets(decorView);
            getWindow().setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mIjkVideoView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mIjkVideoView.resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mIjkVideoView.release();
    }
}
上一篇:3DES加密解密


下一篇:r语言中如何进行两组独立样本秩和检验