Reputation: 396
I have a media player with a seek bar inside of a bottom sheet, and whenever I play whatever sound on the URL that I'm having the seek bar goes just fine but when I close the bottom sheet and open it up again the seek bar reach to 100% of its value and the sounds are still on the beginning.
How do I make the seek bar work when the sound is playing in the background?
Please help me, and thanks in advance.
My fragment
private var mediaPlayer:MediaPlayer? = null
private var oTime = 0
private var sTime: Int = 0
private var eTime: Int = 0
fun quranMp3(){
quranPlay?.setOnClickListener {
// الفاتحة
if (quranPageNum?.equals(0)!!){
if (mediaPlayer == null){
mediaPlayer = MediaPlayer.create(context, Uri.parse("https://server7.mp3quran.net/basit/Almusshaf-Al-Mojawwad/001.mp3"))
Log.d("quranMp3", "ID: ${mediaPlayer!!.audioSessionId}")
val position = mediaPlayer!!.currentPosition/100
mp3SeekBar.progress = position
//totalTime = mediaPlayer!!.duration
eTime = mediaPlayer!!.duration.toLong().toInt()
sTime = mediaPlayer!!.currentPosition.toLong().toInt()
if (oTime == 0) {
quranSeekBar!!.setMax(eTime)
oTime = 1
}
playerDuration?.text = String.format(
"%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(eTime.toLong()),
TimeUnit.MILLISECONDS.toSeconds(eTime.toLong()) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(eTime.toLong())))
playerPosition?.text = String.format(
"%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(sTime.toLong()),
TimeUnit.MILLISECONDS.toSeconds(sTime.toLong()) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(sTime.toLong())))
initializeSeekBar()
}
mediaPlayer?.start()
Log.d("QuranMp3", "Duration: ${mediaPlayer!!.duration/1000} seconds")
}
quranPause?.setOnClickListener {
if (mediaPlayer !== null){
mediaPlayer?.pause()
Log.d("QuranMp3", "Paused at: ${mediaPlayer!!.duration/1000} seconds")
}
mp3SeekBar.setOnSeekBarChangeListener( object : SeekBar.OnSeekBarChangeListener{
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
if (fromUser){
mediaPlayer?.seekTo(progress)
}
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
val Toast = Toast.makeText(context,"تتبع..", Toast.LENGTH_LONG)
Toast.setGravity(Gravity.CENTER_VERTICAL, 50, 50)
Toast.show()
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
val Toast = Toast.makeText(context,"يرجى الانتظار للحصول على البيانات..", Toast.LENGTH_LONG)
Toast.setGravity(Gravity.CENTER_VERTICAL, 50 ,50)
Toast.show()
}
})
private fun initializeSeekBar() {
val mp3SeekBar = view?.findViewById<SeekBar>(R.id.mp3SeekBar)
mp3SeekBar?.max = mediaPlayer!!.duration
val playerPosition = view?.findViewById<TextView>(R.id.player_position)
val playerDuration = view?.findViewById<TextView>(R.id.player_duraation)
val seekbar = this.activity?.getSharedPreferences("QuranPdf", Context.MODE_PRIVATE)
val handler = Handler()
handler.postDelayed(object : Runnable {
override fun run() {
try {
sTime = mediaPlayer!!.currentPosition
playerDuration!!.text = String.format(
"%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(eTime.toLong()),
TimeUnit.MILLISECONDS.toSeconds(eTime.toLong()) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(eTime.toLong())))
playerPosition?.text = String.format(
"%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(sTime.toLong()),
TimeUnit.MILLISECONDS.toSeconds(sTime.toLong()) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(sTime.toLong())))
mp3SeekBar?.progress = sTime //mediaPlayer!!.currentPosition
handler.postDelayed(this, 1000)
} catch (e: Exception) {
mp3SeekBar?.progress = 0
}
}
}, 0)
}
My App Photos
as you can when I open the bottom sheet and press play, everything works fine.
first one
!
but when I close the bottom sheet and open it up again the sound is working but the seek bar and the texts are not working.
second one
Java and Kotlin are acceptable
Upvotes: 0
Views: 522
Reputation: 396
I found the answer to this problem. Apparently, I have to initialize the seekbar in
onResume method.
this is the solution to my question. I only need to add this code
override fun onResume() {
try {
if (mediaPlayer !=null && mediaPlayer!!.isPlaying){
initializeSeekBar()
}
}catch (e : Exception){
e.printStackTrace()
}
super.onResume()
}
Upvotes: 0
Reputation: 12953
You are wrapping the seekbar
updation logic inside mediaplayer == null
condition which will be false
once the MediaPlayer
is initialised.
quranPlay?.setOnClickListener {
// الفاتحة
if (quranPageNum?.equals(0)!!){
if (mediaPlayer == null){
mediaPlayer = MediaPlayer.create(context, Uri.parse("https://server7.mp3quran.net/basit/Almusshaf-Al-Mojawwad/001.mp3"))
}//end it here, rest the code should be outside if condition
//seek bar logic and rest of the code
}
}
Upvotes: 1