记录一下实现抖音列表播放的过程,虽然最后项目叫停了,不过还是记录一下开发的辛酸。
先看下效果图吧,(这个妹子还是挺好看的)
主要说下列表播放的实现吧!
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(); }}
总结 以上是内存溢出为你收集整理的Android 基于dkPlayer实现抖音列表播放全部内容,希望文章能够帮你解决Android 基于dkPlayer实现抖音列表播放所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)