Я обновляю SeekBar, чтобы отразить текущую позицию воспроизведения mp3. Проблема в том, что когда я добираюсь до конца, текущее время превышает общее время на 5-20 секунд. Это соответствующий код.
mViewModel?.getDuration()?.observe(viewLifecycleOwner, Observer {
Log.d(TAG, it.toString())
mBinding?.playerContainer?.totalDuration?.text = msToTime(it)
mBinding?.playerContainer?.currentPosition?.text = msToTime(mViewModel?.mAudioManager?.mediaPlayer?.currentPosition)
})
mViewModel?.getPlayerStatus()?.observe(viewLifecycleOwner, Observer {
if(it == true){
mBinding?.playerContainer?.seekbar?.max = mViewModel?.mAudioManager?.mediaPlayer?.duration!! / 1000
Log.d(TAG, ("maxDuration: ".plus(mViewModel?.mAudioManager?.mediaPlayer?.duration!!)))
}
})
mViewModel?.getPlayingStatus()?.observe(viewLifecycleOwner, Observer {
if(it == true){
val runnable = object : Runnable {
override fun run() {
val currentPosition: Int = mViewModel?.mAudioManager?.mediaPlayer?.currentPosition!! / 1000
mBinding?.playerContainer?.currentPosition?.text = msToTime(mViewModel?.mAudioManager?.mediaPlayer?.currentPosition)
mBinding?.playerContainer?.seekbar?.progress = currentPosition
Log.d(TAG, mViewModel?.mAudioManager?.mediaPlayer?.currentPosition.toString())
mHandler.postDelayed(this, 1000)
}
}
mHandler.post(runnable)
}
})
fun msToTime(stuff: Int?): String {
var duration = stuff!!.toLong()
val hms = String.format(
"%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(duration),
TimeUnit.MILLISECONDS.toMinutes(duration) - TimeUnit.HOURS.toMinutes(
TimeUnit.MILLISECONDS.toHours(
duration
)
),
TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(
duration
)
)
)
return hms
}