当我停止音频并使用playWhenReady重新启动时,该位置会有一点延迟。例如,位置(当我停止音频播放时)为1000。但是当我重新开始更改playWhenReady true时,位置为2000。因此,我的进度栏无法顺利进行。有什么解决办法吗?
if (activeHolder!! == holder) {
when (player!!.playWhenReady) {
true -> {
showLog("player!!.duration, playWhenReady true :"+player!!.duration)
showLog("player!!.currentPosition, playWhenReady true :"+player!!.currentPosition)
player!!.playWhenReady = false
handlerChangeProgress.removeCallbacks(updateChangeProgressTask)
showAudioStartButton(activeHolder!!)
return
}
false -> {
showLog("player!!.duration, playWhenReady false :"+player!!.duration)
showLog("player!!.currentPosition, playWhenReady false :"+player!!.currentPosition)
player!!.playWhenReady = true
handlerChangeProgress.postDelayed(updateChangeProgressTask, delayChangeProgress)
showAudioStopButton(activeHolder!!)
return
}
}
}
override fun onChangeProgressBar(progress: Float, currentPosition: Int, totalDuration: Int) {
if(player!!.playWhenReady) {
if (activeHolder?.adapterPosition == audioPosition) {
activeHolder?.let {
it.binding.progressBar.max = player!!.duration.toInt()
it.binding.progressBar.progress = player!!.currentPosition.toInt()
showAudioStopButton(it)
}
}
}
}
private val updateChangeProgressTask: Runnable = object : Runnable {
override fun run() {
if(player != null && changeProgressPlayerListener != null) {
val totalDuration: Long = player!!.duration
val currentPosition: Long = player!!.currentPosition
val progress = currentPosition.toFloat() / totalDuration.toFloat()
changeProgressPlayerListener!!.onChangeProgressBar(progress, currentPosition.toInt(), totalDuration.toInt())
}
handlerChangeProgress.postDelayed(this, delayChangeProgress)
}
}
Log
D/TAG: player!!.duration, playWhenReady false :4166
D/TAG: player!!.currentPosition, playWhenReady false :2459
D/TAG: player!!.duration, playWhenReady true :4166
D/TAG: player!!.currentPosition, playWhenReady true :2640
当playWhenReady为true时,将进度重置为0。
是您想要的吗?