3

I am using PagerSnapHelper for the recycler view. Now in recycler view items, I have an audio media player in which the audio URL is coming from the API response for a particular item. I want that if the user scroll to another item then the media player object of the previous position should be cleared (stop playing previous) and a new object of that particular new position should be created and set its data source. So what I can do to stop playing the previous item media player?

Here is my logic for onBindViewHolder : here data is the response of API. data.isPlayed is the boolean that I managed for the click of play/pause. data.isInitialized is the boolean that I managed to initialize data for the first time after the user pressed play button.

if (data.isPlayed) {
            btnPlay.setImageResource(R.drawable.ic_pause_circle)
            if (data.isInitialized) {
                data.mediaPlayer?.start()
                data.mediaPlayer?.let { playCycle(binding, it) }
            } else {
                data.mediaPlayer = MediaPlayer()
                data.mediaPlayer?.let {
                    attachMusic(data, binding, it)
                }
                data.isInitialized = true
            }
        } else {
            data.mediaPlayer?.let {
                seekBar.max = it.duration
                seekBar.setProgress(it.currentPosition)
                it.pause()
            }
            btnPlay.setImageResource(R.drawable.ic_play_circle)
        }

Here is some functions that I am using for media player

    private fun attachMusic(data: ClipsResponse.ClipsResponseItem, binding: ItemLayoutCardBinding, mediaPlayer: MediaPlayer) {

    binding.apply {
        try {
            mediaPlayer.setDataSource(data.audioUrl)
            mediaPlayer.prepare()
            Log.d("TAG", "Attaching music")
            setControls(this, mediaPlayer)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        mediaPlayer.setOnCompletionListener {
            btnPlay.setImageResource(R.drawable.ic_play_circle)
            it.release()
        }
    }
}

private fun setControls(binding: ItemLayoutCardBinding, mediaPlayer: MediaPlayer) {
    binding.apply {
        Log.d("TAG", "SetControls")
        seekBar.setMax(mediaPlayer.duration)
        mediaPlayer.start()
        playCycle(this, mediaPlayer)
        if (mediaPlayer.isPlaying) {
            btnPlay.setImageResource(R.drawable.ic_pause_circle)
            playProgressBar.isVisible = false
        }
        seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                if (fromUser) {
                    mediaPlayer.seekTo(progress)
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {}
            override fun onStopTrackingTouch(seekBar: SeekBar) {}
        })
    }
}

private fun playCycle(binding: ItemLayoutCardBinding, mediaPlayer: MediaPlayer) {
    val handler = Handler()
    binding.apply {
        try {
            seekBar.setProgress(mediaPlayer.currentPosition)
            
            if (mediaPlayer.isPlaying) {
                val runnable = Runnable { playCycle(binding, mediaPlayer) }
                handler.postDelayed(runnable, 100)
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }
}

I have seen so many solutions but not any with PageSnapHelper. Does Anyone has a solution then please help me.

Thanks in advance!

krupa parekh
  • 607
  • 3
  • 16

0 Answers0