Android开发StandardGSYVideoPlayer做

2020-12-29  本文已影响0人  你的益达233

二、实现的功能

1.左右切换

用viewpager和fragment做,播放器放在fragment里面

2.控制面板在Activity层,播放时间和播放状态用EventBus传输数据

示例代码:

.setGSYVideoProgressListener { progress, secProgress, currentPosition, duration ->
                isPlay = true
                isPlayComplete = false
                currentPlayPosition = currentPosition
                durationTotal = duration
                this.progress = progress
                //缓存进度条
                this.secProgress = detailPlayer.gsyVideoManager.bufferedPercentage
                LogUtil.i("isBuy2",isBuyTwo+"\t"+currentPosition.toString()+"\t"+secProgress.toString()+"\t"+detailPlayer.gsyVideoManager.bufferedPercentage)
                if (isBuyTwo == "0") {
                    val lookTime = (courseShuBean?.looktime)!!.toLong()//视频可免费观看的时长
                    if (currentPosition/1000 > lookTime) {//超出试看时间
                        detailPlayer.onVideoPause()
                        isPlay = false
                        EventBus.getDefault().post(VideoPlayControlEvent(isPlaying = false))
                        if (trySeeDialog == null ){
                            trySeeDialog = DialogHelper.trySeeOverDialog(context!!)
                        } else if (!trySeeDialog!!.isShowing){
                            trySeeDialog!!.show()
                        }

                    }
                }

                EventBus.getDefault().post(VideoPlayInfoEvent(currentPosition, duration, this.progress, this.secProgress))
            }

着重看EventBus的做的功能即可,其他有试看需求也可以参考下

3.开始播放或者页面切回来重新播放

要用detailPlayer?.startPlayLogic()
示例代码:

override fun setUserVisibleHint(isVisibleToUser: Boolean) {
    super.setUserVisibleHint(isVisibleToUser)

    if (isVisibleToUser) {
         
            if (isPlayComplete){
                detailPlayer?.seekOnStart = 0
                detailPlayer?.startPlayLogic()
            } else {
                detailPlayer?.seekOnStart = currentPlayPosition.toLong()
                detailPlayer?.startPlayLogic()
            }
        


    }
}

4.点击暂停,播放操作

@Subscribe(threadMode = ThreadMode.MAIN)
fun onVideoPlayToFragmentEvent(event: VideoPlayToFragmentEvent) {
    isPlay = event.isPlaying
    if (event.isPlaying) {
        //恢复播放
        //因为拖动进度条重新播放还是未拖动之前的,所以采用屏幕双击没问题的做法
        detailPlayer.touchDoubleUp()
        //detailPlayer.onVideoResume(true)
    } else {
        //停止播放
        detailPlayer.onVideoPause()
        detailPlayer.startDismissControlViewTimer()
    }
}

三、关键点

1、用EventBus来传输数据

2、如果找不到解决方案,看下它的demo,如它的SeekBar的id为progress,
ProgressBar的id为bottom_progressbar去它的父类StandardGSYVideoPlayer再父类,一层层找,找到它原来是怎么引用,怎么赋值的,或许就能做出你要的功能了

3、用EventBus传值到fragment时,记得带上视频id,避免发通知给了之前的fragment,这样会影响到Activity的,反正数据很可能会乱,不行你播放完一个小节,在下小节拖动进度条

这里给出完整代码也很难,如果有同样需求和问题的,可私信,评论。我基本每天都看简书的

上一篇下一篇

猜你喜欢

热点阅读