CurrentPosition of Media player restarts to zero while playing in Kotlin

46 Views Asked by At

In the ViewHolder of my Recycler view I play an audio with Media Player and I show the progress with a Progress Bar. The problem is that suddently the current progress restarts. I don't know why this is happening, and what else to do.

Thanks in advance!!!

LOGCAT

I max duration 15648 I current 0 I current 887 I current 1891 I current 2894 I current 3899 I current 4906 I current 5909 *** I current 6912 I current 1102*** I current 2106 I current 3113

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        val binding = ViewPhraseItemBinding.bind(view)
        private lateinit var audioPhrase: MediaPlayer

        fun render(phraseItem: PhrasesItem, position: Int) {
            binding.tvTitle.text = phraseItem.phrase
            onClickOnPlayer(phraseItem, position)
        }

        private fun onClickOnPlayer(phraseItem: PhrasesItem, position: Int) {
            binding.icPlayer.setOnClickListener {
                when (lastItemPosition){
                    position -> {
                        stopAudio()
                        lastItemPosition = -1
                    }
                    in 0..phrases.size -> {
                        stopAudio()
                        lastItemPosition = position
                        startAudio(phraseItem, position)
                        updateProgressBar()
                    }
                    else -> {
                        startAudio(phraseItem, position)
                        updateProgressBar()
                    }
                }
            }
        }


        private fun startAudio(phraseItem: PhrasesItem, position: Int){
            audioPhrase = MediaPlayer.create(itemView.context, phraseItem.audioId)
            currentAudioPhrase = audioPhrase
            audioPhrase.start()
            phraseItem.isPlaying = true

            binding.icPlayer.setImageResource(R.drawable.ic_pause_circle)
            audioPhrase.setOnCompletionListener {
                binding.icPlayer.setImageResource(R.drawable.ic_play_circle)
                phraseItem.isPlaying = false
            }
            lastItemPosition = position
        }

        private fun stopAudio(){
            currentAudioPhrase?.stop()
            currentAudioPhrase?.release()
            phrases[lastItemPosition].isPlaying = false
            notifyItemChanged(lastItemPosition)
        }

**        private fun updateProgressBar(){
            binding.pbAudioDuration.max = audioPhrase.duration
            Utils.logI("${binding.pbAudioDuration.max}")
            val delayMillis = 1000L // Actualiza cada segundo
            handler.postDelayed(object : Runnable {
                override fun run() {
                    binding.pbAudioDuration.progress = audioPhrase.currentPosition
                    Utils.logI("current ${audioPhrase.currentPosition}")
                    if (audioPhrase.isPlaying) {
                        handler.postDelayed(this, delayMillis)
                    }
                }
            }, delayMillis)
        }**

    }

I searched on the internet, and I tried different ways to get the current position, but none of that worked

0

There are 0 best solutions below